## 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).

correction

``````  extra <- read.table("i_o_files/ex6c_input.txt",
str(extra)``````

2- Read in the same file but, this time, set the argument as.is to TRUE.

Check again the structure: what has changed ?

correction

``````  extra <- read.table("i_o_files/ex6c_input.txt",
as.is=TRUE)
str(extra)``````

3- What are the column names of extra ?

correction

``  colnames(extra)``

4- Change the name of the first column of extra from “State” to “Country”.

correction

``````  # ex6ct all column names of extra
colnames(extra)
# ex6ct the name of the first column only
colnames(extra)[1]
# reassign name of the first column only
colnames(extra)[1] <- "Country"``````

5- How many countries are in the Eurozone, according to extra ?

Remember the table function. ?table

correction

``  table(extra\$Eurozone)``

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

``````  # country names with "c" (lower-case)
grep("c", extra\$Country)
# country names with "c" or "C" (ignoring case)
grep("c", extra\$Country, ignore.case = TRUE)
# show actual country names
grep("c", extra\$Country, value=TRUE, ignore.case = TRUE)``````

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

``  write.table(extra, file="ex6c_output.txt")``
• Add the argument “row.names” set to FALSE.

correction

``````  write.table(extra, file="ex6c_output.txt",
row.names = FALSE)``````
• Add the argument “quote” set to FALSE.

correction

``````  write.table(extra, file="ex6c_output.txt",
row.names = FALSE,
quote = FALSE)``````
• Add the argument “sep” set to “ or to”,"

correction

``````  write.table(extra, file="ex6c_output.txt",
row.names = FALSE,
quote = FALSE,
sep="\t")``````