【问题标题】:How to create a matrix by sequential comparison of dataframe columns in R如何通过顺序比较R中的数据框列来创建矩阵
【发布时间】:2016-02-18 17:57:59
【问题描述】:

我有一个如下的数据框(dput 太长)

      $ OC_AH_026C.chr      : num  1 1 1 1 1 1 1 1 1 1 ...
 $ OC_AH_026C.leftPos  : num  240000 1080000 1200000 1320000 1440000 1800000 2400000 2520000 3120000 3360000 ...
 $ OC_AH_026C.Means    : num  78.1 81.8 156.5 26.8 18.5 ...
 $ OC_AH_026C.UL       : num  125 125 125 125 125 ...
 $ OC_AH_026C.LL       : num  1.95 1.95 1.95 1.95 1.95 ...
 $ OC_AH_026C.res      : num  0 0 1 0 0 0 -1 0 0 0 ...
 $ OC_AH_026C.1.chr    : num  1 1 1 1 1 1 1 1 1 1 ...
 $ OC_AH_026C.1.leftPos: num  240000 1080000 1200000 1320000 1440000 1800000 2400000 2520000 3120000 3360000 ...
 $ OC_AH_026C.1.Means  : num  97.3 88.9 50.1 33.3 44.2 ...
 $ OC_AH_026C.1.UL     : num  125 125 125 125 125 ...
 $ OC_AH_026C.1.LL     : num  2.45 2.45 2.45 2.45 2.45 ...
 $ OC_AH_026C.1.res    : num  0 0 0 0 0 0 0 0 0 0 ...
 $ OC_AH_026T.chr      : num  1 1 1 1 1 1 1 1 1 1 ...
 $ OC_AH_026T.leftPos  : num  240000 1080000 1200000 1320000 1440000 1800000 2400000 2520000 3120000 3360000 ...
 $ OC_AH_026T.Means    : num  12.8 101.7 124 56.1 91.3 ...
 $ OC_AH_026T.UL       : num  126 126 126 126 126 ...
 $ OC_AH_026T.LL       : num  1.83 1.83 1.83 1.83 1.83 ...
 $ OC_AH_026T.res      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ OC_AH_058T.chr      : num  1 1 1 1 1 1 1 1 1 1 ...
 $ OC_AH_058T.leftPos  : num  240000 1080000 1200000 1320000 1440000 1800000 2400000 2520000 3120000 3360000 ...
 $ OC_AH_058T.Means    : num  103 119 201 118 96 ...
 $ OC_AH_058T.UL       : num  124 124 124 124 124 ...
 $ OC_AH_058T.LL       : num  0.684 0.684 0.684 0.684 0.684 ...
 $ OC_AH_058T.res      : num  0 0 1 0 0 0 0 0 0 0 ...

当比较两列与列名中的 res 时,我想获取同一行的 res 编号为 1 或均为 -1 的行数。

我想把它存放在一个矩阵中,这样我就可以得到类似的东西

               OC_AH_026C.res   OC_AH_026C.1.res  OC_AH_026T.res   OC_AH_058T.res
OC_AH_026C.res
OC_AH_026C.1.res
OC_AH_026T.res
OC_AH_058T.res

恐怕我只到了这里,但基本上都错了

    df_list2res <- df_list2[,grep('*.res', names(df_list2))]

    Comparison<-lapply(df_list2res,function(df,col3){
  matches<-df_list2res[which(col3==col3),] #Should compare one column with all the other columns 
  nrow(subset(df_list2res,col != 0))
})

但是对每一列进行逐行比较然后转储到矩阵中的功能打败了我。

编辑

使用有限的输出

structure(list(OC_AH_026C.res = c(0, 0, 1, 0, 0, 0), OC_AH_026C.1.res = c(0, 
0, 0, 0, 0, 0), OC_AH_026T.res = c(0, 0, 0, 0, 0, 0), OC_AH_058T.res = c(0, 
0, 1, 0, 0, 0), OC_AH_084T.res = c(0, 0, 0, 0, 0, 0), OC_AH_086T.res = c(0, 
0, 1, 0, 0, 0)), .Names = c("OC_AH_026C.res", "OC_AH_026C.1.res", 
"OC_AH_026T.res", "OC_AH_058T.res", "OC_AH_084T.res", "OC_AH_086T.res"
), row.names = c(NA, 6L), class = "data.frame")

预期的输出将是(我认为手动完成)

                   OC_AH_026C.res OC_AH_026C.1.res OC_AH_026T.res OC_AH_058T.res OC_AH_084T.res OC_AH_086T.res

OC_AH_026C.res         1            0                 0              1               0               1
OC_AH_026C.1.res       0            0                 0              0               0               0
OC_AH_026T.res         0            0                 0              0               0               0
OC_AH_058T.res         1            0                 0              1               0               1
OC_AH_084T.res         0            0                 0              0               0               0
OC_AH_086T.res         1            0                 0              1               0               1

使用进一步的 dput 输出

    structure(list(OC_AH_026C.res = c(0, 0, 1, 0, 0), OC_AH_026C.1.res = c(0, 
0, 0, 0, 0), OC_AH_026T.res = c(0, 0, 0, 0, 0), OC_AH_058T.res = c(0, 
0, 1, 0, 0), OC_AH_084T.res = c(0, 0, 0, 0, 0), OC_AH_086T.res = c(0, 
0, 1, 0, 0), OC_AH_088T.res = c(1, 1, 0, 1, 0), OC_AH_096T.res = c(0, 
0, 0, -1, 0), OC_AH_100T.res = c(0, 0, 0, 0, 0), OC_AH_127T.res = c(0, 
0, 0, 0, 0), OC_AH_133T.res = c(0, 0, 0, 0, 0), OC_ED_008T.res = c(0, 
0, 1, 0, 0), OC_ED_016T.res = c(0, 0, 0, 0, 0), OC_ED_031T.res = c(0, 
1, 1, 0, 0), OC_ED_036T.res = c(0, 0, 0, 0, 0), OC_GS_001T.res = c(0, 
0, 0, 0, 0), OC_QE_062T.res = c(0, 0, 0, 0, 0), OC_RS_010T.res = c(0, 
0, 0, 0, 0), OC_RS_027C.res = c(0, 0, 1, 0, 0), OC_RS_027C.1.res = c(0, 
0, 1, 0, 0), OC_RS_027T.res = c(0, 0, 1, 0, 0), OC_SH_051T.res = c(0, 
0, 1, 0, 0), OC_ST_014T.res = c(0, 0, 0, 0, 0), OC_ST_016T.res = c(0, 
0, 0, 0, 0), OC_ST_020T.res = c(0, 0, 0, 0, 0), OC_ST_024T.res = c(0, 
0, 0, 0, 0), OC_ST_033T.res = c(0, 0, 0, 0, 0), OC_ST_034C.res = c(0, 
0, 1, 0, 0), OC_ST_034C.1.res = c(0, 0, 0, 0, 0), OC_ST_036T.res = c(0, 
0, 0, 0, 0), OC_ST_037T.res = c(0, 0, 0, 0, 0), OC_ST_040T.res = c(0, 
0, 0, 0, 0), OC_WG_001T.res = c(0, 0, 0, 0, 0), OC_WG_002T.res = c(0, 
0, 0, 0, 0), OC_WG_005T.res = c(0, 0, 0, 0, 0), OC_WG_009T.res = c(0, 
0, 0, 0, 0), OC_WG_019T.res = c(0, 0, 1, 0, 0), Means.res = c(0, 
0, 0, 0, 0), sd.res = c(0, 0, 1, 0, 0)), .Names = c("OC_AH_026C.res", 
"OC_AH_026C.1.res", "OC_AH_026T.res", "OC_AH_058T.res", "OC_AH_084T.res", 
"OC_AH_086T.res", "OC_AH_088T.res", "OC_AH_096T.res", "OC_AH_100T.res", 
"OC_AH_127T.res", "OC_AH_133T.res", "OC_ED_008T.res", "OC_ED_016T.res", 
"OC_ED_031T.res", "OC_ED_036T.res", "OC_GS_001T.res", "OC_QE_062T.res", 
"OC_RS_010T.res", "OC_RS_027C.res", "OC_RS_027C.1.res", "OC_RS_027T.res", 
"OC_SH_051T.res", "OC_ST_014T.res", "OC_ST_016T.res", "OC_ST_020T.res", 
"OC_ST_024T.res", "OC_ST_033T.res", "OC_ST_034C.res", "OC_ST_034C.1.res", 
"OC_ST_036T.res", "OC_ST_037T.res", "OC_ST_040T.res", "OC_WG_001T.res", 
"OC_WG_002T.res", "OC_WG_005T.res", "OC_WG_009T.res", "OC_WG_019T.res", 
"Means.res", "sd.res"), row.names = c(NA, 5L), class = "data.frame")

【问题讨论】:

  • 您能否显示一个小数据集的 dput,即 dput(droplevels(df1[1:6, 1:6])) 以及基于此的预期输出。
  • 我用一个可重现的例子编辑了代码
  • 再试一次,虽然我认为我不能从这个 dput 产生所需的输出,因为我是手动操作的。

标签: r


【解决方案1】:

以下是 this 对您特定问题的回答的改编:

res <- apply(dat, 2, function(x) colSums(x == dat & abs(x) == 1))

> res
                 OC_AH_026C.res OC_AH_026C.1.res OC_AH_026T.res OC_AH_058T.res OC_AH_084T.res OC_AH_086T.res
OC_AH_026C.res                1                0              0              1              0              1
OC_AH_026C.1.res              0                0              0              0              0              0
OC_AH_026T.res                0                0              0              0              0              0
OC_AH_058T.res                1                0              0              1              0              1
OC_AH_084T.res                0                0              0              0              0              0
OC_AH_086T.res                1                0              0              1              0              1

编辑:同样可以使用 lapply:

do.call(rbind,lapply(dat, function(x) colSums(x==dat & abs(x) == 1)))

【讨论】:

  • 谢谢。代码的哪一部分比较两列的同一行。是 x == dat 吗?。
  • 是的,确实如此。 abs(x) 部分检查它是 1 还是 -1。
  • 另外,如果我只想计算同时具有 -1 或都具有 1 的行(而不是两者都具有非零),我对您的代码的编辑是否正确?
  • 我的代码已经这样做了。如果 x 不为 0,则您的编辑代码测试了。因此,在我的代码中,如果 x 为 2,则不会计算在内,但在您的代码中会。
  • 哦。好的,我会改回来的。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多