Saturday, February 7, 2015

R10. Reading a csv table in R

This data is from https://raw.githubusercontent.com/datasets/population/master/data/population.csv. Download the csv file into the working directory.


The read.csv function is used to read the text csv file and return a data.frame object.


When the rows for United States, and the Arab World, are found, we have to include a comma within brackets to indicate we want rows matching the logical condition. Also R puts a period for the space in 'Country Name' so it becomes 'Country.Name'.

# ex10.R
df <- read.csv('population.csv')
print('***class of df =')
print(class(df))
print('***The first 6 rows are')
print(head(df))
print('***The last 6 rows are')
print(tail(df))
print('***The total number of rows is')
print(nrow(df))
df1 <- df[df['Country.Name'] == 'United States',]
df2 <- df[df['Country.Name'] == 'Arab World',]
plot(df2[['Year']],df2[['Value']]/1e6, type = 'l',
     col = 'green', xlab = 'Year',
     ylab = 'Population (millions)',
     main = 'USA - blue, Arab World - green')
lines(df1[['Year']],df1[['Value']]/1e6, col = 'blue')
# [1] "***class of df ="
# [1] "data.frame"
# [1] "***The first 6 rows are"
# Country.Name Country.Code Year     Value
# 1   Arab World          ARB 1960  96388069
# 2   Arab World          ARB 1961  98882541
# 3   Arab World          ARB 1962 101474076
# 4   Arab World          ARB 1963 104169209
# 5   Arab World          ARB 1964 106978105
# 6   Arab World          ARB 1965 109907857
# [1] "***The last 6 rows are"
# Country.Name Country.Code Year    Value
# 12402     Zimbabwe          ZWE 2005 12570686
# 12403     Zimbabwe          ZWE 2006 12529655
# 12404     Zimbabwe          ZWE 2007 12481245
# 12405     Zimbabwe          ZWE 2008 12451543
# 12406     Zimbabwe          ZWE 2009 12473992
# 12407     Zimbabwe          ZWE 2010 12571000
# [1] "***The total number of rows is"
# [1] 12407

Output:

No comments:

Post a Comment