2021年5月6日星期四

R rowSums for multiple groups of variables using mutate and for loops by prefix of variable names

I have multiple variables grouped together by prefixes (par___, fri___, gp___ etc) there are 29 of these groups.

Each variable has a value of 0 or 1. What I need to do is sum these groups (i.e., partner___1 + partner___2 etc) and if the rowSums = 0, make each of the variables NA.

for example. My data looks like this:

par___ par___2 fri___1 fri___2
0 0 1 1
0 1 0 0
0 0 1 0
0 0 0 0

and I want it to look like this:

par___ par___2 fri___1 fri___2
NA NA 1 1
0 1 NA NA
NA NA 1 0
NA NA NA NA

I can do it individually like this:

  df<- df%>%      mutate(rowsum = rowSums(.[grep("par___", names(.))])) %>%       mutate_at(grep("par___", names(.)), funs(ifelse(rowsum == 0, NA, .))) %>%      select(-rowsum)   

And I figured I could do something like this:

vars <- c('par___', "fri___','gp___')      for (i in vars) {    df<- df%>%      # creates a "rowsum" column storing the sum of columns 1:2       mutate(rowsum = rowSums(.[grep(i, names(.))])) %>%       # applies, to columns 1:2, a function that puts NA when the sum of the rows is 0      mutate_at(grep(i, names(.)), funs(ifelse(rowsum == 0, NA, .))) %>%      select(-rowsum)       }  

There are no error messages but it doesn't work.

Also, I've tried mutate(across()) instead of mutate_at() and get this error:

Error: Problem with mutate() input ..1. x Can't convert a list to function i Input ..1 is across(grep(i, names(.)), list(ifelse(rowsum == 0, NA, .))).

And, I've tried list instead of funs and get this error:

Error in rowsum == 0 : comparison (1) is possible only for atomic and list types

Any help would be greatly appreciated!

Thanks heaps.

https://stackoverflow.com/questions/67428040/r-rowsums-for-multiple-groups-of-variables-using-mutate-and-for-loops-by-prefix May 07, 2021 at 09:54AM

没有评论:

发表评论