12.9 Exercise (to do at home)
1- Read in file ex6c_input.txt (that is in folder downloaded for exercice 6 “i_o_files”) in object “extra”
Warning: the file has a header !
Check the structure of extra (remember the str command).
2- Read in the same file but, this time, set the argument as.is to TRUE.
Check again the structure: what has changed ?
3- What are the column names of extra ?
correction
4- Change the name of the first column of extra from “State” to “Country”.
correction
5- How many countries are in the Eurozone, according to extra ?
Remember the table function. ?table
correction
6- In the Eurozone column: change “TRUE” with “yes” and “FALSE” with “no”.
correction
# select the Eurozone column
extra$Eurozone
# elements of the Eurozone column that are exactly TRUE
extra$Eurozone==TRUE
# ex6ct actual values that are TRUE
extra$Eurozone[extra$Eurozone==TRUE]
# reassign all elements that are TRUE with "yes"
extra$Eurozone[extra$Eurozone==TRUE] <- "yes"
# same with FALSE
extra$Eurozone[extra$Eurozone==FALSE] <- "no"
7- In the column Country: how many country names from the list contain the letter “c” (capital- or lower-case) ?
Remember the grep function. Check the help page.
correction
8- According to that data frame: how many people live: + in the European union (whole table) ? + in the Eurozone ?
correction
# sum the whole population column
sum(extra$Population)
# select elements of extra where Eurozone is "yes"
extra$Eurozone == "yes"
# select only elements in Population for which the corresponding Eurozone elements are "yes"
extra$Population[extra$Eurozone == "yes"]
# sum that selection
sum(extra$Population[extra$Eurozone == "yes"])
9- Write extra into file ex6c_output.txt
After each of the following steps, check the output file in the RStudio file browser (lower-right panel).
- Try with the default arguments.
correction
- Add the argument “row.names” set to FALSE.
- Add the argument “quote” set to FALSE.
- Add the argument “sep” set to “ or to”,"