Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
在dplyr中动态更新数据帧的多列_R_Dplyr - Fatal编程技术网

在dplyr中动态更新数据帧的多列

在dplyr中动态更新数据帧的多列,r,dplyr,R,Dplyr,我正在尝试使用dplyr中的mutate\u at更新数据帧的多列。但是,得到一个错误。请帮忙,这是我的密码 library(dplyr)  var1 <- "x9,x10,x11,x12" ## columns that I want to update var2 <- "x8" ## column using which I want to update above columns var3 <- unlist(strsplit(v

我正在尝试使用
dplyr
中的
mutate\u at
更新数据帧的多列。但是,得到一个错误。请帮忙,这是我的密码

library(dplyr) 

var1 <- "x9,x10,x11,x12" ## columns that I want to update
var2 <- "x8" ## column using which I want to update above columns
var3 <- unlist(strsplit(var1, split=","))

df <- structure(list(x8 = 1L, x9 = 0L, x10 = 0L, x11 = 0L, x12 = 
 1L), row.names = c(NA, -1L), class = c("data.frame"), .internal.selfref = '<pointer: 
 0x5621075d0370>') 

# df
#     x8 x9 x10 x11 x12
# 1:  1  0   0   0   1  

# df_output
#     x8 x9 x10 x11 x12
# 1:  1  0   0   0   0 


df %>%  mutate_at(var3, ifelse(var2 == 1, 0, var3)) ## update all the columns in var3 to 0 where value of column var2 == 1
库(dplyr)

var1我们可以跨

library(dplyr)
df %>% 
  mutate(across(all_of(var3), 
      ~ case_when(!! rlang::sym(var2) == 1 ~ 0L, TRUE ~ .)))
-输出

#  x8 x9 x10 x11 x12
#1  1  0   0   0   0