我有一个包含蘑菇特征的数据框,例如:
SPECIES CAP.SHAPE CAP.SURFACE CAP.COLOR BRUISES ODOR GILL.ATTACHMENT GILL.SPACING GILL.SIZE GILL.COLOR
1 PUNGENTIA CONVEX SCALY BROWN YES PUNGENT FREE CLOSE NARROW BLACK
2 YELLOWCAP CONVEX SCALY YELLOW YES ALMOND FREE CLOSE BROAD BLACK
3 BELLSHROOM BELL SCALY WHITE YES ANISE FREE CLOSE BROAD BROWN
STALK.SHAPE STALK.ROOT STALK.SURFACE STALK.COLOR VEIL.COLOR RING.NUMBER RING.TYPE SPORE.PRINT.COLOR POPULATION
1 ENLARGING EQUAL SMOOTH PURPLE WHITE ONE PENDANT BLACK SCATTERED
2 ENLARGING CLUB SMOOTH PURPLE WHITE ONE PENDANT BROWN NUMEROUS
3 ENLARGING CLUB SMOOTH PURPLE WHITE ONE PENDANT BROWN NUMEROUS
HABITAT
1 URBAN
2 GRASSES
3 MEADOWS
我想将一个字符串添加到与CAP,GILL和STALK相关的列的条目中,以便它们的第一个条目成为CAP列的CONVEX.CAP,SCALY.CAP,BROWN.CAP,FREE.GILLS,CLOSE .GILLS,NARROW.GILLS,用于GILL列等。我尝试使用dplyr软件包并使用粘贴来这样做,例如:
reformatted_mushrooms <- mushrooms %>%
mutate_at(.vars = c("CAP.SHAPE", "CAP.SURFACE", "CAP.COLOR"), .funs =
paste("CAP", sep = "."))
这将返回此错误:
Error in get(.x, .env, mode = "function") :
object 'CAP' of mode 'function' was not found
我也尝试使用contains()函数,如下所示:
reformatted_mushrooms <- mushrooms %>%
mutate_at(.vars = contains("CAP"), .funs = paste("CAP"), sep = ".")
但这会产生以下错误:
Error: No tidyselect variables were registered
有人在这里看到我在做什么错吗?非常感谢。
答案 0 :(得分:0)
请确保您在问题中提供可复制的示例!否则,您会付出不必要的努力。
library(tidyverse)
tibble::tibble('CAP.SHAPE' = c(1:5),
'RANDOM.SHAPE' = c(11:15)) %>%
mutate_at(vars(contains("CAP")), ~ paste0(.x, ".CAP"))
A tibble: 5 x 2
CAP.SHAPE RANDOM.SHAPE
<chr> <int>
1 1.CAP 11
2 2.CAP 12
3 3.CAP 13
4 4.CAP 14
5 5.CAP 15