R Calculate Mean Value on CSV data

R is capable to calculate mean value of data on CSV files. Take following weather data example:

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

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
6  Saturday      NA        71    72
7    Sunday      72        72    73
>

Find mean on Morning temperature and skip value “NA” on the list :

> mean(list_of_csv$Morning, na.rm=TRUE)
[1] 70.5
>

Find mean on Afternoon temperature :

> mean(list_of_csv$Afternoon, na.rm=FALSE)
[1] 71.14286

Find mean on Night temperature :

> mean(list_of_csv$Night, na.rm=FALSE)
[1] 70.57143
>

Video