Saturday, February 7, 2015

R11. Frequency table in R

The csv file is downloaded from http://countrylist.net/en/. The delimiter is a semicolon and not a comma.


According to the file, there are 3 countries in Antarctica.


The ftable function is used to organize the data under the category of 'continent'. The $ can be be used since a data.frame is a list, as well. The class of group is 'ftable', and we may extract the names using one of its attributes.

# ex11.R
df <- read.csv('2015-02-07.dump.countrylist.net.csv',
                sep = ";") 
print('***The columns are')
for (i in names(df)) cat(i,'\n')
cat('\n***The number of rows is\n')
print(nrow(df))
cat('\n***Countries in Antartica:\n')
df1 <- df[df['continent'] == 'Antarctica',]
print(df1['name'])
group <- ftable(df$continent)
name <- attr(group, 'col.vars')[[1]]
cat('\n\n***Frequency table:\n')
for (i in 1:length(group)) {
  cat(name[i],group[i],'\n')
}
# [1] "***The columns are"
# id 
# continent 
# name 
# capital 
# iso.2 
# iso.3 
# ioc 
# tld 
# currency 
# phone 
# utc 
# wiki 
# name_de 
# capital_de 
# wiki_de 
# 
# ***The number of rows is
# [1] 250
# 
# ***Countries in Antartica:
#   name
# 11                  Antarctica
# 33               Bouvet Island
# 65 French Southern Territories
# 
# 
# ***Frequency table:
# Africa 62 
# Antarctica 3 
# Asia 55 
# Australia 26 
# Europe 53 
# North America 31 
# South America 20 

No comments:

Post a Comment