Home > OS >  How do i use separate {tidyr} to separate when number occurs?
How do i use separate {tidyr} to separate when number occurs?

Time:11-26

I have a tibble datset where i want to separate a column that also includes one number. I want to speparate them into two columns, though I would like the seperator: "sep" to be equal to a digit. (So that one column would have the number, and the other the fctr.

separate(x, into = c("fctr", "number"), sep ="??what do i put here?")

example: on the "week_days", I want to separate the numbers from the characters. I want the numbers to appear in another column.

enter image description here

CodePudding user response:

Sep should be equal to: "(?<=[A-Za-z])(?=[0-9])".

df <- data.frame(week_days=as.factor(paste0(sample(letters,10),sample(1:10,10))))
separate(df,week_days,into=c("letter","digit"),sep= "(?<=[A-Za-z])(?=[0-9])")

   letter digit
1       u     7
2       a     4
3       m     2
4       k     9
5       b     6
6       t     5
7       h     8
8       v     3
9       r     1
10      i    10
  •  Tags:  
  • r
  • Related