用指数字写入的string和数字导入Excel
我在Excel中有一个数据库,看起来像这样:
Name Note Value1 Value2 Adidas first 1.74E-06 1.06E-07
等等,有成千上万的行和一堆有值的列。
但是,当我使用:
data<-read.xlsx2("data.xlsx",header = T,sheetIndex = 1,colClasses="numeric")
它返回所有的string:
is.numeric(data$X1)
[1] FALSE
到目前为止,我的工作是这样的: as.numeric(as.character(data$X1))
– 但有很多列,它变得非常累人。
我怎样才能直接上传,而不使用这种技巧?
我们可以使用read_excel
library(readx) read_excel('file1.xlsx') # Source: local data frame [1 x 4] # Name Note Value1 Value2 # (chr) (chr) (dbl) (dbl) #1 Adidas first 1.74e-06 1.06e-07
正如我在评论中提到的,如果我们正在使用colClasses
,那么我们需要为整个列指定它
library(xlsx) str(read.xlsx2('file1.xlsx', sheetIndex=1, colClasses=c('character', 'character', 'numeric', 'numeric'), stringsAsFactors=FALSE)) # 'data.frame': 1 obs. of 4 variables: # $ Name : chr "Adidas" # $ Note : chr "first" # $ Value1: num 1.74e-06 # $ Value2: num 1.06e-07