+34 616 71 29 85 carsten@dataz4s.com

Importing from Excel to R

Importing from Excel to R can be done through different R functions.

 

 

readxl package

Using the readxl package and the RStudio menu we can import Excel documents to R. readxl can import .xlsx and the older .xls files. It is a built-in package in RStudio. 

# Loading library
library(readxl)

# Importing from XL with read_excel function
# Saving to the object LungCapData
LungCapData <- read_excel(“C:/Users/Usuario/Documents/dataZ4s/R/ImportExport R-Excel readxl/LungCapData.xlsx”)
View(LungCapData)

This readxl package session is based on Mike Marin and MarinStatsLectures’ video ’Importing/reading Excel data into R using RStudio (readxl)’.

 

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

 

This page is based on MarinStatsLecture’s video: Import Data from Excel to R_csv & txt files.

View the above as Rpubs:  https://rpubs.com/CarstenGrube/622603

 

Carsten Grube

Carsten Grube

Freelance Data Analyst

0 Comments

+34 616 71 29 85

Call me

Spain: Ctra. 404, km 2, 29100 Coín, Malaga

...........

Denmark: c/o Musvitvej 4, 3660 Stenløse

Drop me a line

What are you working on just now? Can I help you, and can you help me? 

About me

Learning statistics. Doing statistics. Freelance since 2005. Dane. Living in Spain. With my Spanish wife and two children. 

What they say

20 years in sales, analysis, journalism and startups. See what my customers and partners say about me.