w3resource

R Programming: Extract Specific Columns from a Data Frame

R Programming: Data frame Exercise-5 with Solution

Write a R program to extract specific column from a data frame using column name.

Sample Solution :

R Programming Code :

 # Create a data frame named 'exam_data' with columns 'name', 'score', 'attempts', and 'qualify'
exam_data = data.frame(
  # Column 'name' with character values
  name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'),
  # Column 'score' with numeric values
  score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
  # Column 'attempts' with numeric values
  attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
  # Column 'qualify' with character values
  qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
)

# Print the message "Original dataframe:"
print("Original dataframe:")

# Print the content of the 'exam_data' data frame
print(exam_data)

# Print the message "Extract Specific columns:"
print("Extract Specific columns:")

# Create a new data frame 'result' with only the 'name' and 'score' columns from 'exam_data'
result <- data.frame(exam_data$name, exam_data$score)

# Print the content of the 'result' data frame
print(result)

Output:

[1] "Original dataframe:"
        name score attempts qualify
1  Anastasia  12.5        1     yes
2       Dima   9.0        3      no
3  Katherine  16.5        2     yes
4      James  12.0        3      no
5      Emily   9.0        2      no
6    Michael  20.0        3     yes
7    Matthew  14.5        1     yes
8      Laura  13.5        1      no
9      Kevin   8.0        2      no
10     Jonas  19.0        1     yes
[1] "Extract Specific columns:"
   exam_data.name exam_data.score
1       Anastasia            12.5
2            Dima             9.0
3       Katherine            16.5
4           James            12.0
5           Emily             9.0
6         Michael            20.0
7         Matthew            14.5
8           Laura            13.5
9           Kevin             8.0
10          Jonas            19.0                         

Explanation:

  • Create a Data Frame:
    • exam_data = data.frame(...)
      • Creates a data frame named exam_data with four columns: name, score, attempts, and qualify.
      • name contains character values representing names.
      • score contains numeric values representing scores.
      • attempts contains numeric values representing the number of attempts.
      • qualify contains character values indicating whether the person qualifies.
  • Print the Original Data Frame:
    • print("Original dataframe:")
      • Prints the message "Original dataframe:" to indicate the start of the data frame output.
    • print(exam_data)
      • Displays the contents of the exam_data data frame.
  • Extract Specific Columns:
    • print("Extract Specific columns:")
      • Prints the message "Extract Specific columns:" to indicate the start of the extraction process.
    • result <- data.frame(exam_data$name, exam_data$score)
      • Creates a new data frame result with only the name and score columns from the exam_data data frame.
    • print(result)
      • Displays the contents of the result data frame, showing only the name and score columns.

    R Programming Code Editor:



    Have another way to solve this solution? Contribute your code (and comments) through Disqus.

    Previous: Write a R program to get the statistical summary and nature of the data of a given data frame.
    Next: Write a R program to extract first two rows from a given data frame.

    Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/r-programming-exercises/dataframe/r-programming-data-frame-exercise-5.php