英文:
How to add a suffix to a sequence of variables in R?
问题
你可以尝试这样修改代码来避免错误:
data <- data %>%
rename_at(vars(AGRA_A00_A09:AGRII_U80_U89), ~paste0(., "_cor"))
这应该能解决你遇到的错误。
英文:
My dataset has over 6k variables and I need to add a suffix to a sequence of variables, but not all of them. I'm trying:
data <- data %>%
rename_at(AGRA_A00_A09:AGRII_U80_U89, ~paste0(., "_cor"))
But I get the following error:
Error: object 'AGRA_A00_A09' not found
I've tried putting quote marks on the variables names and using across, but it still doesn't work. What am I doing wrong?
答案1
得分: 2
Close, use rename_with
:
mtcars %>%
rename_with(disp:drat, .fn = ~ paste0(., "_cor")) %>%
head()
# mpg cyl disp_cor hp_cor drat_cor wt qsec vs am gear carb
# Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
# Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
# Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
# Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
# Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
英文:
Close, use rename_with
:
mtcars %>%
rename_with(disp:drat, .fn = ~ paste0(., "_cor")) %>%
head()
# mpg cyl disp_cor hp_cor drat_cor wt qsec vs am gear carb
# Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
# Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
# Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
# Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
# Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论