简体   繁体   中英

mutate_at using multiple conditons

I would to transform the columns that contain 1 or 2 in their names of my dataframe test by dividing them by the column Unit .

It seems to work with one conditions but I do not know how to add the condition OR 1

 test= test%>% mutate_at(vars(contains('2')), funs(./Unit))

any idea?

Identifier Source 196001     200006  Unit
1:     top    HH   NA        NA      1e-06
2:     top2   BB   NA        4569.6  1e+00

This should work:

test = read.table(header = T,text="
Identifier Source 196001     200006  Unit
top        HH     65         888     3
top2       BB     0111       9886    8") #I modified your values so you can see the divisions

test %>% mutate_at(vars(contains('2'), contains('1')), funs(./Unit))

Basically you say "select variables that contain 2, oh and also select variables than contain 1".

If you wanted to select variables that contain 1 AND 2, this would be a different problem and might require regexp (with dplyr::match ).

Also, please note that funs is soft-deprecated, you should now use lambda functions:

test = test %>% mutate_at(vars(contains('2'), contains('1')), ~./Unit)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM