### Exercise 6. ### # Input / Output # Create the script exercise6.R (in R Studio: File -> ) # and save it to the "Rintro/day3" directory: you will save all the commands of exercise 6 in that script. # Remember you can comment the code using #. ### Exercice 6a. I/O on vectors # 1. Make sure you are in Rintro/day3; if not, go there! getwd() setwd("/nfs/users/bi/sbonnin/Rintro/day3") # 2. Copy directory i_o_files (that is in /nfs/users/bi/sbonnin) # in Rintro/day3. # Use file.copy(): the “recursive” argument should be set to TRUE for directories. file.copy("/nfs/users/bi/sbonnin/i_o_files", ".", recursive = TRUE) # 3. Scan elements from ex6a_input.txt # (that is in the i_o_files directory), and save in object z. # How many elements are in z? z <- scan("i_o_files/ex6a_input.txt") length(z) # 2. Sort elements of z and save into object zsorted. zsorted <- sort(z) # 3. Write zsorted into file ex6a_output.txt. write(zsorted, "ex6a_output.txt") # 4. Check the output; write the file again # setting the argument ncolumns to 1 and see how it differs. write(zsorted, "ex6a_output.txt", ncolumns=1) ### Exercise 6b - I/O on data frame: play with the arguments! ## 1. field separator ## # read ex6b_IO_commas_noheader.txt in object fs. fs <- read.table("i_o_files/ex6b_IO_commas_noheader.txt") # What are the dimensions of fs? dim(fs) # Fields/columns are separated by commas: # change the default value of the sep argument and # read in the file again. fs <- read.table("i_o_files/ex6b_IO_commas_noheader.txt", sep=",") # What are now the dimension of fs? dim(fs) ## 2. field separator + header ## # read ex6b_IO_commas_header.txt in object fs_c. fs_c <- read.table("i_o_files/ex6b_IO_commas_header.txt") # What are the dimensions of fs_c? dim(fs_c) # Change the default field separator. fs_c <- read.table("i_o_files/ex6b_IO_commas_header.txt", sep=",") # The first row is supposed to be the header (column names): # change the default value of the header parameter and # read in the file again. fs_c <- read.table("i_o_files/ex6b_IO_commas_header.txt", sep=",", header=TRUE) # What are now the dimensions of fs_c? ## 3. skipping lines ## # read ex6b_IO_skip.txt in object sk. sk <- read.table("i_o_files/ex6b_IO_skip.txt") # R is complaining about line 2…. # check “manually” the file (in the R Studio file browser for example). # The skip argument allows you to ignore lines before reading # in a file. Introduce this argument and read the file again. sk <- read.table("i_o_files/ex6b_IO_skip.txt", skip=2) # What are the dimensions of sk? dim(sk) # Change the default field separator. sk <- read.table("i_o_files/ex6b_IO_skip.txt", skip=2, sep=",", header=T) # What are now the dimensions of sk? dim(sk) ## 4. comment lines ## # read ex6b_IO_comment.txt in object cl. cl <- read.table("i_o_files/ex6b_IO_comment.txt") # R is complaining (again!): check manually the file # and try to find out what is wrong. # Adjust the comment.char argument and read the file again. cl <- read.table("i_o_files/ex6b_IO_comment.txt", comment.char = "*") # Adjust also the header and sep arguments to read in # the file correctly. cl <- read.table("i_o_files/ex6b_IO_comment.txt", comment.char = "*", sep=",", header=TRUE) # What are now the dimensions of cl? dim(cl) ## 4. final ## # read ex6b_IO_final.txt in object fin. fin <- read.table("i_o_files/ex6b_IO_final.txt") # Adjust the appropriate parameters to obtain the # data frame fin of dimensions 167 x 4. fin <- read.table("i_o_files/ex6b_IO_final.txt", sep=",", header=TRUE, skip=3, comment.char="&" ) ### Exercice 6c – I/O on data frame ### # 1. Read in file ex6c_input.txt # (that is in i_o_files) in object ex6. # The file has a header! ex6 <- read.table("i_o_files/ex6c_input.txt", header=TRUE) # 2. Check the structure of ex6. str(ex6) # 3. Now read in the same file but, this time, # set the argument as.is to TRUE. ex6 <- read.table("i_o_files/ex6c_input.txt", header=TRUE, as.is=TRUE) # Check again the structure: what has changed? str(ex6) # 4. What are the column names of ex6? colnames(ex6) # 5. Change the name of the first column of ex6 to "Country". colnames(ex6)[1] <- "Country" # 6. How many countries are in the Eurozone, # according to this table? table(ex6$Eurozone) # 7. Reassign "TRUE" with "yes" and "FALSE" with "no" # in the Eurozone column. ex6$Eurozone[ex6$Eurozone==TRUE] <- "yes" ex6$Eurozone[ex6$Eurozone==FALSE] <- "no" # 8. How many country names contain the letter “c”? # Remember the grep function. Check the help page. grep("c", ex6$Country) grep("c", ex6$Country, value=TRUE) # 9. How many people live, according to that table: #in the European union (whole table)? #in the Eurozone? sum(ex6$Population) sum(ex6$Population[ex6$Eurozone == "yes"]) # 10. Write ex6 into file “ex6c_output.txt”. #Try with the default arguments. write.table(ex6, file="ex6c_output.txt") #Add row.names set to FALSE. write.table(ex6, "ex6c_output.txt", row.names = FALSE) #Add quote set to FALSE. write.table(ex6, "ex6c_output.txt", row.names = FALSE, quote = FALSE) #Add sep set to "\t" or to "," write.table(ex6, "ex6c_output.txt", row.names = FALSE, quote = FALSE, sep="\t") write.table(ex6, "ex6c_output.txt", row.names = FALSE, quote = FALSE, sep=",") #Check the output at each step.