按行检查数据帧,在多列上检查,代码1检查为正数,0检查为负数

时间:2018-06-18 15:53:17

标签: r data-manipulation

我有一组列,所有列都被编码为因子。这些值编码为1表示正数,0表示负数。行上的样本,以及列上每个的分数。

如果有任何积极因素,我想知道,样本明智。如果至少有一个阳性,我想在同一个数据库中生成一个新的列,表示为1,因为此样本中至少有一个为正,或者为0,因为此样本对所有人都是负数。

    dat3 <- structure(list(A = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L), .Label = c("1", "0"), class = "factor"),
                   B = structure(c(1L,1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,1L, 1L, 1L, 1L, 2L, 1L, 1L), .Label = c("0", "1"), class = "factor"),
                   C = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L), .Label = c("nd","0", "1"), class = "factor"),
                   D = structure(c(1L, 1L, 1L,2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L,2L, 1L, 1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"),
                   E = structure(c(1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0","1"), class = "factor")),
                  .Names = c("A", "B", "C", "D", "E"), class = "data.frame", row.names = c(NA, 24L))

我尝试使用if和if else语句实现了我想要的结果,但它们真的很乏味,我认为这不是最好的方法。我一直在尝试应用功能,但我没有取得多大成功。

我期待的结果是

dat3$result <- c(0,0,1,1,1,1,0,1,0,1,0,1,1,0,1,0,1,1,1,0,0,1,0,1)

1 个答案:

答案 0 :(得分:1)

&lt; dat3&#39;列都是AutoSize,可以转换为Label,然后使用true创建二进制列

ListView

或转换为逻辑矩阵,然后执行Anchor

factor