Importing from Excel to R

This is a run through of MarinStatsLecture’s video Import from Excel to R, csv & txt files

This page is part of ‘My notes on R statistical programming’ which I publish on my site: https://dataz4s.com/

Saving files as Comma separated values: .csv Tab delimited text files: .txt

read.csv

read.csv(file, header = TRUE, sep = “,”, quote = “"”, dec = “.”, fill = TRUE, comment.char = "", …)

# Get help
help("read.csv")
## starting httpd help server ... done
?read.csv
data1 <- read.csv(file.choose(), header = T)
data1
##    LungCap Age Height Smoke Gender Caesarean
## 1    6.475   6   62.1    no   male        no
## 2   10.125  18   74.7   yes female        no
## 3    9.550  16   69.7    no female       yes
## 4   11.125  14   71.0    no   male        no
## 5    4.800   5   56.9    no   male        no
## 6    6.225  11   58.7    no female        no
## 7    4.950   8   63.3    no   male       yes
## 8    7.325  11   70.4    no   male        no
## 9    8.875  15   70.5    no   male        no
## 10   6.800  11   59.2    no   male        no

read.table

data2 <- read.table(file.choose(), header = T, sep = ",")
data2
##    LungCap Age Height Smoke Gender Caesarean
## 1    6.475   6   62.1    no   male        no
## 2   10.125  18   74.7   yes female        no
## 3    9.550  16   69.7    no female       yes
## 4   11.125  14   71.0    no   male        no
## 5    4.800   5   56.9    no   male        no
## 6    6.225  11   58.7    no female        no
## 7    4.950   8   63.3    no   male       yes
## 8    7.325  11   70.4    no   male        no
## 9    8.875  15   70.5    no   male        no
## 10   6.800  11   59.2    no   male        no

read.delim

data3 <- read.delim(file.choose(), header = T)
data3
##    LungCap Age Height Smoke Gender Caesarean
## 1    6.475   6   62.1    no   male        no
## 2   10.125  18   74.7   yes female        no
## 3    9.550  16   69.7    no female       yes
## 4   11.125  14   71.0    no   male        no
## 5    4.800   5   56.9    no   male        no
## 6    6.225  11   58.7    no female        no
## 7    4.950   8   63.3    no   male       yes
## 8    7.325  11   70.4    no   male        no
## 9    8.875  15   70.5    no   male        no
## 10   6.800  11   59.2    no   male        no

read.table

data4 <- read.table(file.choose(), header = T, sep = "\t")
data4
##    LungCap Age Height Smoke Gender Caesarean
## 1    6.475   6   62.1    no   male        no
## 2   10.125  18   74.7   yes female        no
## 3    9.550  16   69.7    no female       yes
## 4   11.125  14   71.0    no   male        no
## 5    4.800   5   56.9    no   male        no
## 6    6.225  11   58.7    no female        no
## 7    4.950   8   63.3    no   male       yes
## 8    7.325  11   70.4    no   male        no
## 9    8.875  15   70.5    no   male        no
## 10   6.800  11   59.2    no   male        no