在R中的嵌套数据帧内多次调用ifelse

在R中的嵌套数据帧内多次调用ifelse,r,nested,dataframe,lapply,R,Nested,Dataframe,Lapply,我有一个如下形式的数据框: 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

我有一个如下形式的数据框:

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
几天前,我学会了命令:

with(LociDT4Length[[1]], ifelse(Cohort=="RA", V1/62,
                         ifelse(Cohort=="Lupus", V1/62,
                         ifelse(Cohort=="CEU", V1/96,
                         ifelse(Cohort=="YRI", V1/80,NA)))))
适当地返回结果:

[1] 2.468750 3.274194 4.806452 3.475000
但是,我尝试将此语句放入循环中时,对于每个嵌套的DF都会返回一个警告,并返回不正确的结果。错误消息是:

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.
或者我想使用lappy将此语句应用于此嵌套数组中的46个嵌套DFs

有什么建议吗?如果ifelse语法很糟糕,很笨拙,我也愿意改变它

非常感谢。

试试这个

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)
这应该起作用:

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)))))
要避免嵌套的
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)]))

非常感谢,这是一个非常简单的构造,用于通过lapply合并任何未来函数,这最终是这个问题的一个基本目标)。
#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)]))