Search⌘ K

Solution Review: Handling CSV file

Explore how to handle CSV files in R by reading data into data frames, accessing specific columns, and writing processed data back to files. This lesson equips you with practical skills for managing data input and output using R's file handling functions.

Solution: Input/Output from File

R
path <- "data.csv"
csvData = read.csv(path)
items <- csvData$Item
write.csv(items, "output/outData.csv")

Explanation

In the code snippet above, we first fetch the data of the .csv file in csvData.

The data fetched from read.csv() is directly in the format of a data frame. Therefore, csvData is a data frame. Now, we can excess one of the columns Item using the syntax:

csvData$Item

We store this column in the variable items and then write on file output/outData.csv.