Unlocking the Power of CSV Files in R
Getting Started with CSV Files
A CSV (Comma Separated Value) file is a plain text file that uses commas to separate values. R provides a seamless way to read and write CSV files, making it easy to work with data.
Reading a CSV File in R
Let’s dive into an example. Suppose we have a CSV file named airtravel.csv
containing monthly air travel data in thousands of passengers for 1958-1960. To read this file, we use the read.csv()
function, which creates a dataframe that we can store in a variable.
R
read_data <- read.csv("airtravel.csv")
print(read_data)
If the file is located elsewhere, we need to specify the path along with the file name.
Understanding Your Data
Once we’ve read the CSV file, we can use various functions to explore our data. For instance, we can find the total number of rows and columns using nrow()
and ncol()
.
R
ncol(read_data) # returns 4
nrow(read_data) # returns 12
Finding Extremes with min() and max()
We can also use min()
and max()
to find the minimum and maximum values in a specific column.
R
min(read_data$1960) # returns 390
max(read_data$1958) # returns 505
Extracting Data with Subset()
The subset()
function allows us to extract data that meets certain conditions. For example, we can extract data where the 1958 column has values greater than 400.
R
sub_data <- subset(read_data, read_data$1958 > 400)
print(sub_data)
Writing to a CSV File in R
To write data to a CSV file, we use the write.csv()
function, passing the data in the form of a dataframe.
R
dataframe1 <- data.frame(...)
write.csv(dataframe1, "file1.csv")
By default, write.csv()
wraps values in double quotes. To remove these quotes, we can pass the quote = FALSE
argument.
R
write.csv(dataframe1, "file1.csv", quote = FALSE)
With these essential skills, you’re now ready to harness the power of CSV files in R!