My goal is to create a new variable that matches the output from fun(5) for each row but right now fun(1:5) does not give the desired output. Please advise me on how to create a new function that allows me to perform this operation in a vectorized manner since looping it would take a very long time.
I would like f(1:5) to produce a vector of 5 elements, each one matching the output given from f(5) corresponding to each row.
Thanks
names1 = c("x","y","z")
names2 <- c("hi","hello","bye")
fun <- function(x){paste(names2[which(df[x,names1] == 1)],collapse = " ")}
df <- data.frame(a = c(0,1,2,3,4),x = c(0,0,1,0,1),y = c(0,1,0,0,1),z = c(0,1,0,1,0))
fun(5) = "hi hello"
fun(1:5) = "bye NA NA NA NA NA"