When using XLConnect
to read in a .xls file how do you set missing values. Similar to the na.string==c()
argument in read.csv
.
当使用XLConnect读取.xls文件时,如何设置丢失的值。在read.csv中类似于na.string==c()参数。
My current R code looks like this:
我现在的R代码是这样的:
install.packages("XLConnect")
library(XLConnect)
excel.file <- file.path(".../FakeData.xls")
fake2 <- readWorksheetFromFile(excel.file, sheet="Sheet1", header=TRUE)
My fake data look like this:
我的假数据是这样的:
ID Age Weight Gender
1 30 55 M
2 22 NA F
3 33 80 NA
I want my NA values to be interpreted as missing, so that Weight and Gender are read into R as numeric/factor variables, respectively. Currently, they are being converted into character variables because of the NA values.
我希望我的NA值被解释为缺失,以便将权重和性别分别读入R作为数值/因子变量。目前,由于NA的值,它们被转换为字符变量。
When I try:
当我尝试:
setMissingValue(fake2, value = c("NA"))
I get the following error:
我得到以下错误:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘setMissingValue’ for signature ‘"data.frame"’
1 个解决方案
#1
5
You have to call setMissingValue
on a workbook object not on the filename.
您必须在工作簿对象上调用setMissingValue而不是在文件名上。
wb <- loadWorkbook(excel.file)
setMissingValue(wb, value = c(""))
fake2 <- readWorksheet(wb, sheet="Sheet1", header=TRUE)
#1
5
You have to call setMissingValue
on a workbook object not on the filename.
您必须在工作簿对象上调用setMissingValue而不是在文件名上。
wb <- loadWorkbook(excel.file)
setMissingValue(wb, value = c(""))
fake2 <- readWorksheet(wb, sheet="Sheet1", header=TRUE)