In R, tabular data is the most common data-type. For instance, you may have prepared data in an excel sheet and not you want to upload it into R. While there are R packages that can read xlxs files, we will work with the simpler csv (“comma separated values”) format. In a csv file each column is separated by a comma. For the purpose of this code, create a simple table in Excel and save it in your working directory as a csv file called “myfile”.
# Here I created a very simple table in Excel with three countries and some sample data and saved it as myfile.csv. I then load it into R.
As part of R’s inbuilt functions you can upload that csv file with the read.csv( ) command.
myfile <- read.csv("myfile.csv",header = TRUE, sep = ",",row.names=1)
To check that your data uploaded correctly, you can check its first couple of entries with the head() function.
head(myfile)
##
Data 1 | Data 2 | Data 3 | |
---|---|---|---|
Germany | 5 | 2 | 8 |
Canada | 6 | 4 | 5 |
Spain | 3 | 6 | 7 |
To save that file in your working directory, use write.csv( ).
write.csv(myfile,file="mynewfile.csv")
access_time Last update May 8, 2020.