Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中使用for循环和mutate函数生成新变量_R_Loops_For Loop_Dplyr_Mutate - Fatal编程技术网

在R中使用for循环和mutate函数生成新变量

在R中使用for循环和mutate函数生成新变量,r,loops,for-loop,dplyr,mutate,R,Loops,For Loop,Dplyr,Mutate,假设我有一个如下所示的数据帧: fact_code style_serial ss rib button rib_s button_s 1008 style_1018 1 0 0 1 1 1008 style_1018 0 1 0 1 1 1008 style_1018 0 1 0 1 1 1008 style_1018 0 0 1 1 1

假设我有一个如下所示的数据帧:

fact_code style_serial ss rib button rib_s button_s
1008      style_1018   1   0  0      1     1 
1008      style_1018   0   1  0      1     1
1008      style_1018   0   1  0      1     1
1008      style_1018   0   0  1      1     1 
1008      style_1003   1   0  1      0     1
1008      style_1003   0   0  1      0     1
1008      style_1003   0   0  0      0     1
1008      style_1003   0   0  0      0     1
1004      style_1197   1   0  0      1     0 
1004      style_1197   0   0  0      1     0
1004      style_1197   0   0  0      1     0
1004      style_1197   0   1  0      1     0
关键变量rib和button是虚拟变量。它们表明工厂生产的特定服装款式是否有罗纹或纽扣,或两者兼有。然后我想取这些伪变量的最大值,这些伪变量按
fact\u code
style\u serial
分组,在这种情况下,我将它们命名为
rib\u s
button\u

变量
rib_s
button_s
生成如下:

df <- df %>% group_by(fact_code, style_serial) %>% mutate(rib_s = max(rib, na.rm = TRUE))
df <- df %>% group_by(fact_code, style_serial) %>% mutate(button_s = max(button, na.rm = TRUE))
df%分组依据(事实代码,样式序列)%>%变异(肋骨=最大值(肋骨,na.rm=真))
df%分组依据(事实代码,样式序列)%>%变异(按钮=最大值(按钮,na.rm=真))
现在假设我有大约20个这样的变量。我想创建一个循环,该循环的运行次数与变量数量相同,并且每次都为20个伪变量中的每一个执行上述代码

作为测试,我尝试了这两个变量:

for (xx in c("rib", "button")){
df <- df %>%
group_by_(fact_code, style_serial) %>%
yy <- paste0(c(xx, "s"), collapse = "_") %>%
mutate_(yy = max(xx, na.rm = TRUE))
}
for(xx in c(“肋骨”、“按钮”)){
df%
分组依据(事实代码、样式序列)%>%
yy%
变异(yy=max(xx,na.rm=TRUE))
}
但它给了我以下错误信息:

使用方法中的错误(“变异”): 没有适用于“字符”类对象的“mutate”方法

我也尝试过基本r函数,例如
tapply
aggregate
,但总是收到一些错误消息


你有办法解决这个问题吗

使用
dplyr::mutate_at
可以非常简洁地解决这个问题:

library(dplyr)
key <- c("rib", "button")
df %>%
    group_by(fact_code, style_serial) %>%
    mutate_at(vars(key), funs(max = max(.)))
## A tibble: 12 x 9
## Groups:   fact_code, style_serial [3]
#   fact_code style_serial    ss   rib button rib_s button_s rib_max button_max
#       <int> <fct>        <int> <int>  <int> <int>    <int>   <dbl>      <dbl>
# 1      1008 style_1018       1     0      0     1        1      1.         1.
# 2      1008 style_1018       0     1      0     1        1      1.         1.
# 3      1008 style_1018       0     1      0     1        1      1.         1.
# 4      1008 style_1018       0     0      1     1        1      1.         1.
# 5      1008 style_1003       1     0      1     0        1      0.         1.
# 6      1008 style_1003       0     0      1     0        1      0.         1.
# 7      1008 style_1003       0     0      0     0        1      0.         1.
# 8      1008 style_1003       0     0      0     0        1      0.         1.
# 9      1004 style_1197       1     0      0     1        0      1.         0.
#10      1004 style_1197       0     0      0     1        0      1.         0.
#11      1004 style_1197       0     0      0     1        0      1.         0.
#12      1004 style_1197       0     1      0     1        0      1.         0.
钥匙
df <- read.table(text =
    "fact_code style_serial ss rib button rib_s button_s
1008      style_1018   1   0  0      1     1
1008      style_1018   0   1  0      1     1
1008      style_1018   0   1  0      1     1
1008      style_1018   0   0  1      1     1
1008      style_1003   1   0  1      0     1
1008      style_1003   0   0  1      0     1
1008      style_1003   0   0  0      0     1
1008      style_1003   0   0  0      0     1
1004      style_1197   1   0  0      1     0
1004      style_1197   0   0  0      1     0
1004      style_1197   0   0  0      1     0
1004      style_1197   0   1  0      1     0", header = T)