[英]Multiple calls to ifelse within a nested dataframe in R
I have a dataframe of the form: 我有一个表格的数据框:
LociDT4Length
[[1]]
Cohort V1
1: CEU 237
2: Lupus 203
3: RA 298
4: YRI 278
[[2]]
Cohort V1
1: CEU 625
2: Lupus 569
3: RA 1022
4: YRI 762
[[3]]
Cohort V1
1: CEU 161
2: Lupus 203
3: RA 268
4: YRI 285
[[4]]
Cohort V1
1: CEU 1631
2: Lupus 1363
3: RA 1705
4: YRI 1887
A few days ago, I learned the command: 几天前,我学会了这个命令:
with(LociDT4Length[[1]], ifelse(Cohort=="RA", V1/62,
ifelse(Cohort=="Lupus", V1/62,
ifelse(Cohort=="CEU", V1/96,
ifelse(Cohort=="YRI", V1/80,NA)))))
which appropriately returns results: 适当地返回结果:
[1] 2.468750 3.274194 4.806452 3.475000
However, my attempts to put this statement into a loop returned one warning for each nested DF as well as returning incorrect results. 但是,我尝试将此语句置于循环中会为每个嵌套的DF返回一个警告,并返回不正确的结果。 The error message was:
错误消息是:
1: In `[<-.data.table`(x, j = name, value = value) :
Coerced 'double' RHS to 'integer' to match the column's type; may have
truncated precision. Either change the target column to 'double' first
(by creating a new 'double' vector length 4 (nrows of entire table) and
assign that; i.e. 'replace' column), or coerce RHS to 'integer' (e.g. 1L,
NA_[real|integer]_, as.*, etc) to make your intent clear and for speed.
Or, set the column type correctly up front when you create the table and
stick to it, please.
So, I would like to either figure out how to entice R to apply this statement repeatedly using a statement like: 所以,我想要弄清楚如何使用如下语句重复应用此语句:
for (i in 1:length(LociDT4Length)){
with(LociDT4Length[[i]], ifelse(Cohort=="RA", V1/62,
ifelse(Cohort=="Lupus", V1/62,
ifelse(Cohort=="CEU", V1/96,
ifelse(Cohort=="YRI", V1/80,NA)))))
}
or I would like to use lapply to apply this statement over the 46 nested DFs in this nested array. 或者我想使用lapply将此语句应用于此嵌套数组中的46个嵌套DF。
Any suggestions? 有什么建议么? If the ifelse syntax is poor and clunky, I am open to changing this as well.
如果ifelse语法很差且很笨重,我也愿意改变它。
Thanks very much. 非常感谢。
This should work: 这应该工作:
lapply(LociDT4Length, function(x)
with(x,ifelse(Cohort %in% c("RA","Lupus"), V1/62,
ifelse(Cohort=="CEU", V1/96,
ifelse(Cohort=="YRI", V1/80,NA)))))
To avoid nested ifelse
try this: 要避免嵌套
ifelse
尝试以下操作:
#define cohort and matching divisor
origin=c("RA","Lupus","CEU","YRI")
divisor=c(62,62,96,80)
#avoid ifelse
lapply(LociDT4Length, function(x)
with(x,V1/divisor[match(Cohort,origin)]))
Try this 尝试这个
myFun = function(x){with(x, ifelse(Cohort=="RA", V1/62,
ifelse(Cohort=="Lupus", V1/62,
ifelse(Cohort=="CEU", V1/96,
ifelse(Cohort=="YRI", V1/80,NA)))))}
results = lapply(LociDT4Length, myFun)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.