[英]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
.我想通过将它们除以列
Unit
来转换在我的数据帧test
的名称中包含 1 或 2 的列。
It seems to work with one conditions but I do not know how to add the condition OR 1
它似乎适用于一个条件,但我不知道如何添加条件
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".基本上你说“选择包含 2 的变量,哦,还选择包含 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
).如果您想选择包含 1 AND 2 的变量,这将是一个不同的问题,可能需要正则表达式(使用
dplyr::match
)。
Also, please note that funs
is soft-deprecated, you should now use lambda functions:另外,请注意
funs
已被软弃用,您现在应该使用 lambda 函数:
test = test %>% mutate_at(vars(contains('2'), contains('1')), ~./Unit)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.