1

May I ask how to split the column of a data frame into multiple columns, for example:

ID  value
10.A.S  1
11.A.S  2
12.A.S  3
10.A    4
11.A    5
12.A    6

I want to split the ID column based on the ".", and the expected result should be like:

ID  NO.    type treatment value
10.A.S  10  A      S        1
11.A.S  11  A      S        2
12.A.S  12  A      S        3
10.A    10  A               4
11.A    11  A               5
12.A    12  A               6

Thank you very much.

Bio_farmer
  • 149
  • 1
  • 7

1 Answers1

2

An option is separate. The sep in separate takes by default regex. According to ?separate

sep - If character, is interpreted as a regular expression. The default value is a regular expression that matches any sequence of non-alphanumeric values.

The . is a metacharacter to match any character. So, we either escape (\\.) or place it in square brackets ([.])

library(dplyr)
library(tidyr)
df1 %>%
      separate(ID, into = c("NO.", "type", "treatment"), 
                 sep="\\.", remove = FALSE, convert = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662