R 不能与数据表一起使用“应用”
我有一个数据帧,我用下面的方式读入RR 不能与数据表一起使用“应用”,r,R,我有一个数据帧,我用下面的方式读入R morbiditydata <-fread("data.csv", nrows=502647, select=c(2458:2486)) 我们可以使用data.table方法 nm1 <- names(morbiditydata)[1:28] morbiditydata[, (nm1) := lapply(.SD, function(x) as.integer(x %in% c(1065, 1072))), .SDcols =
morbiditydata <-fread("data.csv", nrows=502647, select=c(2458:2486))
我们可以使用
data.table
方法
nm1 <- names(morbiditydata)[1:28]
morbiditydata[, (nm1) := lapply(.SD, function(x)
as.integer(x %in% c(1065, 1072))), .SDcols = nm1]
使用OP的示例“df”
nm1 <- names(df)[1:2]
setDT(df)[, (nm1) := lapply(.SD, function(x)
as.integer(x %in% c(1065, 1072))) ,.SDcols = nm1]
df
# a b c
#1: 0 0 9999
#2: 0 1 1065
#3: 1 1 1072
#4: 1 1 1072
#5: 0 0 0
#6: 0 0 1
nm1它是一个数据表
。使用morbiditydata[,1:28,with=FALSE]
对列进行子集设置。感谢您的帮助。然而,当我使用apply(morbiditydata[,1:28,with=FALSE],1,函数(x){ifelse(x==1065 | x==1072,“1”,“0”)}时,它会说:警告消息:1:In[@RossM我还发布了一个示例数据集,因为您没有显示任何数据。我在您的数据上尝试了它,对我来说,它返回了与以前相同的错误:警告消息:1:In[
nm1 <- names(morbiditydata)[1:28]
morbiditydata[, (nm1) := lapply(.SD, function(x)
as.integer(x %in% c(1065, 1072))), .SDcols = nm1]
t(apply(morbiditydata[,1:28, with = FALSE], 1,
function(x) {ifelse(x==1065 | x==1072, "1","0")}))
nm1 <- names(df)[1:2]
setDT(df)[, (nm1) := lapply(.SD, function(x)
as.integer(x %in% c(1065, 1072))) ,.SDcols = nm1]
df
# a b c
#1: 0 0 9999
#2: 0 1 1065
#3: 1 1 1072
#4: 1 1 1072
#5: 0 0 0
#6: 0 0 1
set.seed(24)
morbiditydata <- as.data.table(matrix(sample(c(1065,
1072, 1:5), 5*30, replace=TRUE), ncol=30))