我希望在这里找到一个解决方案,在逐行的基础上决定3-4列(应用?),并在新列df$NAME中分配最高值列的名称。下面是我想要的结果;最好的方法是什么?谢谢!
| Bird | Cat | Dog | NAME
| 3 | 4 | 10 | DOG
| 5 | 2 | 4 | BIRD
| 3 | 6 | 2 | CAT
| 4 | 8 | 9 | DOG发布于 2016-07-12 23:41:10
Is there something like a pmax index?非常适合您。
在简单性和效率方面,max.col()-based解决方案(链接问题中的第二个答案)优于apply()-based解决方案。
df$NAME <- names(df)[max.col(df)];
df;
## Bird Cat Dog NAME
## 1 3 4 10 Dog
## 2 5 2 4 Bird
## 3 3 6 2 Cat
## 4 4 8 9 DogData
df <- data.frame(Bird=c(3L,5L,3L,4L),Cat=c(4L,2L,6L,8L),Dog=c(10L,4L,2L,9L));https://stackoverflow.com/questions/38332862
复制相似问题