Math.factor(j) error when using data.table created from .xlsx import in R -


i using information base data.table pull data other data.tables in following example:

test <- function() {     library(data.table)      test.dt <-     data.table(id=c("abc","xyz","ijk"),type=c("1","1","0"),line.position=1:3)     counts.dt <- data.table(             abc=c(10,na,na,na),xyz=c(20,30,na,na),ijk=c(10,10,10,10),x2abc=na,x3abc=1:4)      print(test.dt)     print(counts.dt)     test.dt[,count:=sum(!is.na(counts.dt[[id]])),by=id]     test.dt[,count.value:=counts.dt[line.position,id,with=false],by=id]     print(test.dt) } 

this works fine, , returns expected result: column pulls uses (line.position,id) row in test.dt grab values of counts(line.position,id).

however, cannot repeat more complex example pulls data worksheet. error: error in math.factor(j) : abs not meaningful factors. error thrown right before last print statement.

test2 <- function(     file.directory="c:/users/csnyder/desktop/bootmethod/",     file.name="test.xlsx",       resample.number=3     )  {     require("pbsmapping")     require("xlsx")     library(data.table)      #load input sheets     file.path<-sprintf("%s%s",file.directory,file.name)     excel.data<-read.xlsx(file.path,sheetindex=1,header=true,stringsasfactors=true)     data.dt<-data.table(excel.data)     excel.data<-read.xlsx(file.path,sheetindex=2,header=true,stringsasfactors=true)     base.dt<-data.table(excel.data)     excel.data<-read.xlsx(file.path,sheetindex=3,header=true,stringsasfactors=true)     related.dt<-data.table(excel.data)     excel.data<-null      #add max rows each id type. with=true, colnames used variables.     #get.text<-function(x){return(as.character(x))}     base.dt<-base.dt[,max.sample:= sum(!is.na(data.dt[[id]]),na.rm=true),by=id]      base.length<-nrow(base.dt)     base.dt[,sub.number:=1:base.length]     base.dt[,resample:=1]     resample.base.dt<-base.dt      #add line numbers data tables.     data.dt[,line:=1:nrow(data.dt)]     related.dt[,line:=1:nrow(related.dt)]      #resample number added base dt, make loop resample numbers             , loop it.     for(counter in 1:resample.number){         base.dt<-rbindlist(list(base.dt,resample.base.dt[,resample:=counter]))     }     #remove loop initiator     base.dt<-base.dt[-(1:base.length)]     #number rows     base.dt[,row.number:=resample*base.length+sub.number-base.length]      #pick line sample     pick.row<-function(x){return(runif(1,1,x))}     base.dt[,"line":=runif(1,1,max.sample),with=false]     base.dt[,"line":=round(runif(1,1,max.sample),digits=0),by=row.number]      #pull cell data.dt (and related.dt) has position corresponding         matching row.number , id in base.dt     base.dt[,from.data:=data.dt[line,id,with=false],by=id]     print(base.dt) } 

now, sheets excel workbook import looks (to me @ least) following:

sheet1:

data.dt<-data.table(item1=c("aaaa","2xxx",780,684,614,39),item2=c("aaaa","xxx",10,314,na,na)) 

sheet2:

base.dt<-data.table(id=c("item1","item2"),level=c("x2xxx","xxx"),type=c("aaaa","aaaa"),p=c(1000,1000    ),cat=c("aaaa","aaaa"),day=c(na,1)) 

sheet3:

related.dt<-data.table(item1=c("aaaa","2xxx",1,1,1,na),item2=c("aaaa","xxx",1,1,na,na)) 

at current location, can't upload workbook. replacing excel imports direct calls above seems fix problem. @ risk of not having reproducible question, have ask: has run problem or have idea of how resolve it? or perhaps i'm going in convoluted way--work-arounds equally welcomed! if excel workbook needed understand question, let me know , i'll try best upload one.

here's when 1 gets error:

abs(as.factor(5)) # error in math.factor(as.factor(5)) : abs not meaningful factors 

you have factors because of stringsasfactors = true in read's , because 1 or more of elements in 1 of columns thought numbers, not number, string. check of columns factors, running

sapply(dt, class) 

and take there.


edit arun: should note when converting, say, 5 factor number, should first convert character using as.character , numeric or integer using as.numeric or as.integer:

x <- factor(5)  # correct conversion as.numeric(as.character(x)) # [1] 5  # incorrect conversion if want number coerced numeric type as.numeric(x) # gets levels of factor numeric instead # [1] 1 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -