通过从列表

时间:2016-04-12 10:49:45

标签: r

我有一个大型数据集,我想循环结果,将列减去列表中的列,并输出新列中每行的结果。

ref1 <- samples   Controls
       E_2334188 E_2334207
       E_2334202 E_2334221

df1 <- 
      Chr  Start End    Feature         E_2334188   E_2334202   E_2334207    E_2334221
      1 740001 760000 1:740001-760000    1.6832013  0.8346011  -0.23045394  1.5974912
      1 760001 780000 1:760001-780000   -0.3231613 -1.8504905   0.13668752 -0.38662600
      1 780001 800000 1:780001-800000   -0.3936060 -2.2163153  -0.15266541 -0.60706691



ind <- which(names(df1) %in% ref1$samples)
rnd <- which(names(df1) %in% ref1$controls)

    df2 <- df1[,c(1:4)]  
    df2$newcol <- 0

    for (i in 1:nrow(ref1)){
        n <- df1[ind]-df1[rnd]
        df2$newcol[i] <- n
    }

预期结果

df2 <- 


         Chr  Start End    Feature         E_2334188   E_2334202   
          1 740001 760000 1:740001-760000    1.913655  -0.7628901  
          1 760001 780000 1:760001-780000    -0.4598488    -1.463865
          1 780001 800000 1:780001-800000   -0.2409406 -1.609248

1 个答案:

答案 0 :(得分:1)

我们可以根据'samples'和'Controls'中的元素对'df1'进行子集,减去它们,并使用'df1'的前4列cbind

cbind(df1[1:4],df1[ref1$samples]- df1[ref1$Controls])
#   Chr  Start    End         Feature  E_2334188  E_2334202
#1   1 740001 760000 1:740001-760000  1.9136552 -0.7628901
#2   1 760001 780000 1:760001-780000 -0.4598488 -1.4638645
#3   1 780001 800000 1:780001-800000 -0.2409406 -1.6092484

注意:如果“示例”和“控件”列为factor类,请转换为character并使用相同的方法。

cbind(df1[1:4],df1[as.character(ref1$samples)]- df1[as.character(ref1$Controls)])