r - Select a group of columns, evaluate each cell, create new variable based on evaluation, append to dataset using dplyr -


this question has answer here:

i have dataframe want select several columns out of , check, row row, if columns greater 3. part working.

then want create 1/0 flag condition true , add full dataset. can dplyr, until have resort cbind() add new variable. there dpylr way this?

code 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) 

i have found dply solution on own. following code solves it:

fsurv %>% mutate(fall.flourisher=ifelse(!rowsums(select(.,one_of(vars)) < 4),1,0)) 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -