假设我有一个简单的数据框:
到目前为止看起来很好。然后...
test_df <- data.frame(c(0,0,1,0,0,1,1,1,1,1),c(1,0,0,0,0,0,0,0,0,0))
我希望能够找出每一行中的最大数字(0或1)。例如,在我的例子中,第一个向量的最大值为1(有6次出现),第二个向量的最大值为0(有9次出现)。
我开始的代码如下:
> sapply(test_df,table)
c.0..0..1..0..0..1..1..1..1..1. c.1..0..0..0..0..0..0..0..0..0.
0 4 9
1 6 1
到目前为止看起来很好。然后...
> sapply((sapply(test_df,table)),max)
[1] 4 6 9 1
我迷失了,我丢失了联想?1 -> 6,0 -> 9 我想要返回一个“获胜者”的向量:1,0,...
1 for the first vector (6 occurrences)
0 for the second vector (9 occurrences)
...
data.frame
时,您将得到c.0..0..1..0..0..1..1..1..1..1
作为列名。请检查您创建的“test_df”的输出。 - akrun