I have a data frame and I want to create a new variable applying a function that works within rows. See the example below.
library(tidyverse)
n <- 100
z0 <- data.frame(A = sample(c("y","n",NA), n, replace = T, prob = c(.4,.4,.1)),
B = sample(c("y","n",NA), n, replace = T, prob = c(.4,.4,.1)),
C = sample(c("y","n",NA), n, replace = T, prob = c(.4,.4,.1)))
The functions is as follows
z0 %>% apply(1, function(x) any("y" == x)) -> z0$new
The desired output is z0$new
, and I would like to do it using mutate
rather than apply
.
Thanks for any sugestion