R Read CSV File

CSV or comma separated value is convenient way to to store data in tabular format. You may already dealt with this type of file format before. Fortunately, R is capable to read CVS file and assignment value to variable. Let see how to deal with weather data below

Day Morning Afternoon Night
Monday 67 69 65
Tuesday 71 71 70
Wednesday 72 73 72
Thursday 71 71 71
Friday 70 71 71

Assign csv file to variable ‘list_of_csv’ by use a ‘read.csv’ command:

> list_of_csv <- read.csv(file="convertcsv.csv",head=TRUE,sep=",")
> list_of_csv
        Day Morning Afternoon Night
1    Monday      67        69    65
2   Tuesday      71        71    70
3 Wednesday      72        73    72
4  Thursday      71        71    71
5    Friday      70        71    71
>

To get more information about ‘read.csv()’ command, you can use ‘help(read.csv)’ in R console. An R documentation window will pop up contain full information about this command.

> help(read.csv)

Sometimes you want to information about header of the table. You can extract header information by use command ‘name(vaiable)’:

> names(list_of_csv)
[1] "Day"       "Morning"   "Afternoon" "Night"    
>

If you want to import just certain number of rows from CSV file set nrows paramater:

> list_of_csv <- read.csv(file="convertcsv.csv",nrows=2)
> list_of_csv
      Day Morning Afternoon Night
1  Monday      67        69    65
2 Tuesday      71        71    70
>