0

I have a dataframe that I want to select several columns out of and then check, row by row, if all those columns are greater than 3. This part is working.

Then I want to create a 1/0 flag to say this condition is true and then add that back to the FULL dataset. I can do everything with dplyr, until I have to resort to an cbind() to add the new variable. Is there a dpylr way to do this?

Code is as follows:

vars=as.character(paste("sect4q",letters[c(1,4,5,6:16)],sep=""))

fsurv=structure(list(surveyid = 41:44, ID = c(90002, 90078, 90097, 
90120), sect4qa = c(4, 5, 4, 4), sect4qb = c(4, 5, 3, 5), sect4qc = c(4, 
5, 3, 5), sect4qd = c(4, 5, 5, 5), sect4qe = c(4, 5, 3, 5), sect4qf = c(4, 
5, 4, 4), sect4qg = c(5, 5, 5, 5), sect4qh = c(2, 4, 4, 4), sect4qi = c(4, 
4, 4, 4), sect4qj = c(2, 4, 4, 3), sect4qk = c(4, 5, 4, 5), sect4ql = c(4, 
5, 3, 5), sect4qm = c(4, 5, 5, 5), sect4qn = c(4, 5, 4, 5), sect4qo = c(4, 
5, 4, 4), sect4qp = c(4, 5, 4, 5)),.Names = c("surveyid", "ID", "sect4qa", "sect4qb", "sect4qc","sect4qd","sect4qe", "sect4qf", "sect4qg", "sect4qh", "sect4qi", "sect4qj", "sect4qk", "sect4ql", "sect4qm", "sect4qn", "sect4qo", "sect4qp"), class = "data.frame", row.names =c(NA, 4L))

fsurv2= fsurv %>%
select(one_of(vars)) %>%
transmute(fall.flourisher=ifelse(!rowSums(. < 4),1,0))

fsurv=cbind(fsurv,fsurv2)
David Eagle
  • 285
  • 4
  • 11

1 Answers1

1

I have found the dply solution on my own. The following code solves it:

fsurv %>%
mutate(fall.flourisher=ifelse(!rowSums(select(.,one_of(vars)) < 4),1,0))
David Eagle
  • 285
  • 4
  • 11