Reputation: 295
fn <- function(D, e) {
for(i in 1:nrow(D)) {
if(D[i,1] == e) {
print("y")
}
}
}
fn(events, "a")
Problem: events
is a 2-by-n matrix. If instead of "a"
, I want to pass c("a","b","c")
in e
, then how to check if(D[i,1]==e)
condition?
Upvotes: 1
Views: 3868
Reputation: 70653
I would approach it like this:
x <- matrix(sample(letters, 25), ncol = 5)
> x
[,1] [,2] [,3] [,4] [,5]
[1,] "k" "v" "n" "l" "f"
[2,] "w" "c" "y" "r" "i"
[3,] "u" "p" "o" "q" "j"
[4,] "g" "s" "d" "t" "x"
[5,] "a" "z" "b" "h" "m"
e <- c("r", "e", "d")
apply(x, 1, function(x, e) any(x %in% e), e = e)
[1] FALSE TRUE FALSE TRUE FALSE
Upvotes: 2