AndyApps
AndyApps

Reputation: 57

Read Excel in R

In excel I have a table that looks like this:

`    Data Freq
1 [35-39]    1
2 [40-44]    3
3 [45-49]    5
4 [50-54]   11
5 [55-59]    7
6 [60-64]    7`

I'm trying to figure out a way of being able to read the value in the Data column as the intervals for calculations in the R Project software.

I need to calculate things as:

`(39-35)/2`

Upvotes: 0

Views: 1111

Answers (3)

Robert
Robert

Reputation: 5152

xlsx uses rJava and needs Java. An alternative is readxl

library(readxl)

ed=read_excel("myfile.xlsx")

Upvotes: 0

cagliari2005
cagliari2005

Reputation: 271

Another way that doesn't use libraries is to convert you excel file into a csv (via save as in excel) and then read the data using read.csv.

Upvotes: 1

Petr Matousu
Petr Matousu

Reputation: 3140

# read
library(xlsx)
d <- read.xlsx('data.xlsx',header=T,sheetIndex=1)
# reorder
dl <- do.call(rbind,strsplit(as.character(d$Data),split='-|\\[|\\]'))
d$b <- as.numeric(dl[,3])
d$a <- as.numeric(dl[,2])
# calculate
d$mid <- (d$b-d$a)/2+d$a

Upvotes: 2

Related Questions