I have a data frame that looks like this:
x <- data.frame(A = c("a", "b", "c", "d", "e"),
> B_x = c("1,0", "1,0", "1,2", "2,2", "2,1"),
> C_x = c("0,1", "1,0", "1,2", "2,2", "2,0"),
> D_x = c("1,1", "1,2", "2,0", "1,0", "0,1"))
>
> x
> A B_x C_x D_x
> 1 a 1,0 0,1 1,1
> 2 b 1,0 1,0 1,2
> 3 c 1,2 1,2 2,0
> 4 d 2,2 2,2 1,0
> 5 e 2,1 2,0 0,1
I want to separate the columns ending with "_x" , so the resulting data frame will be:
> A B_x_y B_x_z C_x_y C_x_z D_x_y D_x_z
> 1 a 1 0 0 1 1 1
> 2 b 1 0 1 0 1 2
> 3 c 1 2 1 2 2 0
> 4 d 2 2 2 2 1 0
> 5 e 2 1 2 0 0 1
I tried to use both tidyr::separate()
and select()
to specify the columns I would like to split
x %>% separate(select(ends_with("_x")), into = c("_y", "_z"), sep = ",")
But got the following error message:
Error in
separate()
:
! Problem while evaluatingselect(ends_with(".x"))
.
Caused by error inUseMethod()
:
! no applicable method for 'select' applied to an object of class "c('integer', 'numeric')"
I also tried separate_wider_delim()
but not didn't work as well. Not sure if separate() is the correct way to go...
Any suggestions are appreciated! Thank you!