I've got a dataframe containing features of mushrooms, as such:
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
I want to append a string to the entries of the columns relating to CAP, GILL and STALK, so that their first entries become CONVEX.CAP, SCALY.CAP, BROWN.CAP for the CAP columns, FREE.GILLS, CLOSE.GILLS, NARROW.GILLS, for the GILL columns and so on. I tried to do so with the dplyr package, and by using paste, like so:
reformatted_mushrooms <- mushrooms %>%
mutate_at(.vars = c("CAP.SHAPE", "CAP.SURFACE", "CAP.COLOR"), .funs =
paste("CAP", sep = "."))
This however returns this error:
Error in get(.x, .env, mode = "function") :
object 'CAP' of mode 'function' was not found
I also tried using the contains() function, like this:
reformatted_mushrooms <- mushrooms %>%
mutate_at(.vars = contains("CAP"), .funs = paste("CAP"), sep = ".")
But this yields the following error:
Error: No tidyselect variables were registered
Does anyone see what I'm doing wrong here? Much appreciated.
Please make shure you provide a reproducible example in your question! Otherwise, you'll create unnecessary effort.
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