0

I have a tibble where I want to separate a column that also includes one number. I want to separate it into two columns. 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

Mata
  • 538
  • 3
  • 17
Chrisabe
  • 65
  • 5
  • 1
    Welcome to SO! Can you provide a reproducible data set (you can use `dput`)? – Maël Nov 25 '21 at 15:25
  • Hey, dont know if I am able to reproduce.. hehe. Though I provided a picture. Thank you! – Chrisabe Nov 25 '21 at 15:58
  • Does this answer your question? [split character data into numbers and letters](https://stackoverflow.com/questions/9756360/split-character-data-into-numbers-and-letters) – Maël Nov 25 '21 at 16:14

1 Answers1

0

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
Maël
  • 45,206
  • 3
  • 29
  • 67