temp = structure(list(name1 = structure(c(2L, 1L, 2L, 1L, 2L), .Label = c("Joe",
"Mike"), class = "factor"), name2 = c("Nick", "Matt", "Nick",
"Matt", "Nick"), name3 = c("Matt", "Tom", "Tom", "Steve", "Tom"
)), .Names = c("name1", "name2", "name3"), row.names = c(NA,
-5L), class = "data.frame")
大家好,
我有一个关于 R 编程的看似简单的问题。请参考下面的数据框,其代码如上所示:
name1 name2 name3
1 Mike Nick Matt
2 Joe Matt Tom
3 Mike Nick Tom
4 Joe Matt Steve
5 Mike Nick Tom
我想要一个简单的函数,它返回一个布尔向量,指示特定名称是否出现在这个数据框的一行中(在任何列中)。例如:
myfunction(Matt)
# should return
c(TRUE, TRUE, FALSE, TRUE, FALSE).
由于Matt出现在第1、2和第4行,对此的任何简单帮助都将不胜感激,谢谢!
Reduce(\
|`, lapply(temp, `==`, "Matt"))`。 - thelatemailrowSums
orReduce
are also about 20 times faster than looping over each row inapply
, and about 60 times faster than usingby_row
indplyr/purrr
- thelatemail