R Programming - Extract 3rd and 5th Rows with 1st and 3rd Columns from a Data Frame
Write a R program to extract 3rd and 5th rows with 1st and 3rd columns from a given data frame.
Sample Solution :
R Programming Code :
# Create a data frame named exam_data with four columns: name, score, attempts, and qualify
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'),
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19),
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1),
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 3rd and 5th rows with 1st and 3rd columns:"
print("Extract 3rd and 5th rows with 1st and 3rd columns :")
# Extract and print the 3rd and 5th rows and the 1st and 3rd columns of the data frame
result = exam_data[c(3,5),c(1,3)]
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 3rd and 5th rows with 1st and 3rd columns :" name attempts 3 Katherine 2 5 Emily 2
Explanation:
- Create Data Frame:
- exam_data = data.frame(...): Creates a data frame named exam_data with four columns: name, score, attempts, and qualify.
- name: A vector of student names.
- score: A vector of scores corresponding to the students.
- attempts: A vector of the number of attempts each student made.
- qualify: A vector indicating whether each student qualified or not.
- Print Original Data Frame:
- print("Original dataframe:"): Prints a message to indicate that the following output is the original data frame.
- print(exam_data): Displays the exam_data data frame.
- Extract Specific Rows and Columns:
- print("Extract 3rd and 5th rows with 1st and 3rd columns :"): Prints a message indicating that the following output will show the extraction of specific rows and columns.
- result = exam_data[c(3,5),c(1,3)]: Extracts the 3rd and 5th rows and the 1st and 3rd columns from the exam_data data frame and assigns it to the result variable.
- c(3,5): Specifies the 3rd and 5th rows.
- c(1,3): Specifies the 1st and 3rd columns.
- Print Extracted Data:
- print(result): Displays the extracted subset of the data frame, showing the 3rd and 5th rows with only the 1st and 3rd 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 extract first two rows from a given data frame.
Next: Write a R program to add a new column in a given data frame.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics