R Programming: Save and Display Data Frame information in file
Write a R program to save the information of a data frame in a file and display the information of the file.
Sample Solution:
R Programming Code:
# Create a data frame named 'exam_data' with columns: name, score, attempts, and qualify
exam_data = data.frame(
name = c('Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'), # Define the 'name' column with student names
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19), # Define the 'score' column with corresponding scores
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1), # Define the 'attempts' column with number of attempts
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes') # Define the 'qualify' column with qualification status
)
# Print a message indicating the original data frame
print("Original dataframe:")
# Display the original 'exam_data' data frame
print(exam_data)
# Save the 'exam_data' data frame to a file named 'data.rda'
save(exam_data, file = "data.rda")
# Load the data from the file 'data.rda' into the current R session
load("data.rda")
# Get information about the file 'data.rda' (such as size, type, and permissions)
file.info("data.rda")
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 size isdir mode mtime ctime data.rda 344 FALSE 644 2018-10-25 12:06:09 2018-10-25 12:06:09 atime uid gid uname grname data.rda 2018-10-25 12:06:09 1000 1000 trinket trinket
Explanation:
- Create a data frame:
- A data frame named exam_data is created with four columns: name, score, attempts, and qualify.
- Each column contains a set of values:
- name: Names of the students.
- score: Corresponding scores for each student.
- attempts: Number of attempts made by each student.
- qualify: Indicates whether the student qualified (yes or no).
- Print a message:
- "Original dataframe:" is printed to indicate the data frame's content is displayed next.
- Display the data frame:
- The exam_data data frame is printed to show its contents.
- Save the data frame to a file:
- The exam_data data frame is saved to a file named data.rda using the save() function.
- Load the data from the file:
- The data saved in the file data.rda is loaded back into the R environment using the load() function.
- Display file information:
- The file.info() function displays information about the file data.rda, such as its size, permissions, and last modification time.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to find elements come only once that are common to both given data frames.
Next: Write a R program to count the number of NA values in a data frame column.
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