仅将一列嵌套的data.frames除以整数值

时间:2014-06-03 08:23:47

标签: r dataframe nested lapply

我希望将数据帧的第三列除以5.这些数据帧是嵌套的,如下所示:

[[44]]
    Ethnicity      Variant  Sum
 1:       ASW     ACCEPTOR    1
 2:       ASW          CDS   68
 3:       ASW   CGA_CNVWIN 1000
 4:       ASW     CGA_MIRB    0
 5:       ASW       DELETE    0
 6:       ASW      DISRUPT    0
 7:       ASW        DONOR    0
 8:       ASW   FRAMESHIFT    0
 9:       ASW       INSERT    1
10:       ASW       INTRON   54

我使用了三个命令,每个命令都成功但具有脱靶效果。

lapply(ASWldtSUM,function(x)(x/5))

返回

[[44]]
    Ethnicity Variant   Sum
 1:        NA      NA   0.2
 2:        NA      NA  13.6
 3:        NA      NA 200.0
 4:        NA      NA   0.0
 5:        NA      NA   0.0

这会导致将所有行除以5的不幸影响,导致类在$ Sum列中不是整数时出现问题。

lapply(ASWldtSUM,function(x[,3])(x/5))

具有仅返回单个向量的效果,如果这不是嵌套的数据帧数组,则可以很好地工作,但语句

ASWdtSUM$NEWCOL<-lapply(ASWldtSUM,function(x[,3])(x/5))

不能简单地写,因为它是嵌套的。

使用rapply,如以下语句中所示:

rapply(ASWldtSUM,function(x) if (is.integer(x)) {(x/5)})

导致结果无序化。

那么,是否有一种简单的方法可以将第4列附加到每个嵌套的DataFrame,或者将每个DF(Sum)的第三列替换为该值除以5?

2 个答案:

答案 0 :(得分:2)

这很简单,如果ASWldtSUM是包含数据框的列表的名称,那么你可以这样做:

lapply(ASWldtSUM,FUN=function(x) { x[,3]=x[,3]/5; return(x) })

基本上你用(整个)第三列的分数替换(整个)第三列。

在实践中:

> ASWldtSUM1=data.frame(Ethnicity=rep('ASW',10),Variant=c("ACCEPTOR","CDS","CGA_CNVWIN","CGA_MIRB","DELETE","DISRUPT","DONOR","FRAMESHIFT","INSERT","INTRON"), Sum=c(1,68,1000,0,0,0,0,0,1,54))
> #created a first data.frame (equal to your example)
> ASWldtSUM2=data.frame(Ethnicity=rep('ASW',10),Variant=c("ACCEPTOR","CDS","CGA_CNVWIN","CGA_MIRB","DELETE","DISRUPT","DONOR","FRAMESHIFT","INSERT","INTRON"), Sum=c(1,2,3,4,5,6,7,8,9,10))
> #created a second data.frame (with different values for the third column)
> ASWldtSUM=list(ASWldtSUM1,ASWldtSUM2)
> #created a list of data frames
> lapply(ASWldtSUM,FUN=function(x) { x[,3]=x[,3]/5; return(x) })
> #apply the function to divide third column to each nested data.frame
[[1]]
   Ethnicity    Variant   Sum
1        ASW   ACCEPTOR   0.2
2        ASW        CDS  13.6
3        ASW CGA_CNVWIN 200.0
4        ASW   CGA_MIRB   0.0
5        ASW     DELETE   0.0
6        ASW    DISRUPT   0.0
7        ASW      DONOR   0.0
8        ASW FRAMESHIFT   0.0
9        ASW     INSERT   0.2
10       ASW     INTRON  10.8

[[2]]
   Ethnicity    Variant Sum
1        ASW   ACCEPTOR 0.2
2        ASW        CDS 0.4
3        ASW CGA_CNVWIN 0.6
4        ASW   CGA_MIRB 0.8
5        ASW     DELETE 1.0
6        ASW    DISRUPT 1.2
7        ASW      DONOR 1.4
8        ASW FRAMESHIFT 1.6
9        ASW     INSERT 1.8
10       ASW     INTRON 2.0
> #desired result

答案 1 :(得分:1)

有很多方法可以做到这一点。这是一个:

创建一些示例数据:

dat <- lapply(1:3, function(x)data.frame(a=sample(letters, 4), b=sample(LETTERS, 4), z=rnorm(4)))

dat
[[1]]
  a b          z
1 r M  0.3054329
2 v I -0.8051859
3 t Q -1.6082701
4 u D -0.2315290

[[2]]
  a b          z
1 j W -0.4692469
2 f S  0.3112689
3 a D  0.4208704
4 w Z  0.6903139

[[3]]
....

接下来,在lapply()中使用一个小的匿名函数。为了更好地说明,我乘以100而不是除以5:

lapply(dat, function(x){x[3] <- x[3]*100; x})

[[1]]
  a b          z
1 r M   30.54329
2 v I  -80.51859
3 t Q -160.82701
4 u D  -23.15290

[[2]]
  a b         z
1 j W -46.92469
2 f S  31.12689
3 a D  42.08704
4 w Z  69.03139

[[3]]
....