我有一个看起来像这样的数据框:
ID age sex chem1 chem2 chem3 ... chem524
01 64 m .06 6.8 .3 .2
02 57 f .7 24.3 NA .7
03 53 f .4 2.9 .03 1.6
04 68 m .7 37.8 .01 .01
05 73 m 1.2 1.4 2.8 3.6
06 49 f .3 7.6 .3 2.9
我需要为每种化学品制作3小块瓷砖。我知道如何一次针对一种化学物质执行此操作,但是我不想写524次。
我希望将每种化学物质的小样一起存储在一个新的数据框中,以便稍后对它们进行进一步的分析。
这是我希望输出数据帧看起来像的样子:
ID age sex chem1 chem2 chem3 ... chem524
01 64 m 1 2 2 1
02 57 f 2 3 NA 2
03 53 f 2 1 1 3
04 68 m 2 3 1 1
05 73 m 3 1 3 3
06 49 f 1 2 2 3
这是我尝试过的:
df2 <- mutate_at(df, vars(chem1:chem524), ntile(top_air[4:528], 3))
这没有用,因为看起来ntile只能作用于一列。我也尝试使用Apply,但是我也无法使它正常工作。
谢谢您的帮助!
Here's one approach with
mutate_at
:The proper parameterization of the
.funs =
argument is a bit specialized, but for a single function just supply a one element list starting with~
.~
is shorthand for an anonymous function with one argument which we can access with.
.您还可以命名列表以获取新列:
Another way is to skip the anonymous function all together and pass the additional arguments to
ntile
with the...
part ofmutate_at
: