Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中使用从.xlsx导入创建的data.table时出现Math.factor(j)错误_R_Data.table_Xlsx - Fatal编程技术网

在R中使用从.xlsx导入创建的data.table时出现Math.factor(j)错误

在R中使用从.xlsx导入创建的data.table时出现Math.factor(j)错误,r,data.table,xlsx,R,Data.table,Xlsx,我正在使用base data.table中的信息从其他data.table中提取数据,如下例所示: 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

我正在使用base data.table中的信息从其他data.table中提取数据,如下例所示:

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)
}

test以下是出现错误的时间:

abs(as.factor(5))
# Error in Math.factor(as.factor(5)) : abs not meaningful for factors
之所以有因子,是因为
read
中的
stringsAsFactors=TRUE
,并且因为其中一列中的一个或多个元素实际上不是数字,而是字符串。通过运行,检查哪些列是因子

sapply(dt, class)
从那里开始


从Arun编辑:您应该注意,当将例如
5
因子
转换为数字时,您应该首先使用
as.character
将其转换为字符,然后使用
as.numeric
as.integer
将其转换为数字或整数:

x <- factor(5)

# correct conversion
as.numeric(as.character(x))
# [1] 5

# incorrect conversion if you want the number coerced to numeric type
as.numeric(x) # gets the levels of factor numeric instead
# [1] 1

x以下是出现错误的时间:

abs(as.factor(5))
# Error in Math.factor(as.factor(5)) : abs not meaningful for factors
之所以有因子,是因为
read
中的
stringsAsFactors=TRUE
,并且因为其中一列中的一个或多个元素实际上不是数字,而是字符串。通过运行,检查哪些列是因子

sapply(dt, class)
从那里开始


从Arun编辑:您应该注意,当将例如
5
因子
转换为数字时,您应该首先使用
as.character
将其转换为字符,然后使用
as.numeric
as.integer
将其转换为数字或整数:

x <- factor(5)

# correct conversion
as.numeric(as.character(x))
# [1] 5

# incorrect conversion if you want the number coerced to numeric type
as.numeric(x) # gets the levels of factor numeric instead
# [1] 1

x经验教训:从excel导入可能会导致意外类型。使用eddi和Arun的建议,我能够找出我所有列的类型,并确保它们与data.table的标题类型(即字符)匹配。数学因素错误让我发疯,因为我在寻找一个因素问题;这确实是一个类型不匹配的问题。谢谢你的帮助!对于那些可能遇到同样问题的人,直到我实施了eddi和arun的解决方案,这才起作用。转换为字符和数字是关键。经验教训:从excel导入可能会导致意外类型。使用eddi和Arun的建议,我能够找出我所有列的类型,并确保它们与data.table的标题类型(即字符)匹配。数学因素错误让我发疯,因为我在寻找一个因素问题;这确实是一个类型不匹配的问题。谢谢你的帮助!对于那些可能遇到同样问题的人,直到我实施了eddi和arun的解决方案,这才起作用。转换为字符和数字是关键。
x <- factor(5)

# correct conversion
as.numeric(as.character(x))
# [1] 5

# incorrect conversion if you want the number coerced to numeric type
as.numeric(x) # gets the levels of factor numeric instead
# [1] 1