1

How can I split the column rn into two columns with Base R? I tried strsplit(schluempfe$rn, ".", fixed=TRUE) which splits successfully but I do not know how to get two columns with this function. Do I need to bind them back with cbind()? If this is not possible I will revert to separate() or str_split_fixed() but it "seems simple enough" for Base R.

> str(schluempfe)
Classes ‘data.table’ and 'data.frame':  13534 obs. of  2 variables:
 $ rn    : chr  "oberschlumpf.2020-05-13" "oberschlumpf.2020-05-12" 
"oberschlumpf.2020-05-11" "oberschlumpf.2020-05-10" ...
 $ reCNru: num  15.9 19.2 25.2 21.3 18.6 ...
 - attr(*, ".internal.selfref")=<externalptr> 

As output I would like to see

Classes ‘data.table’ and 'data.frame':  13534 obs. of  3 variables:
 $ rn1   : chr  "oberschlumpf" "oberschlumpf" "oberschlumpf" "oberschlumpf" ...
 $ rn2   : chr  "2020-05-130" "2020-05-12" "2020-05-11" "2020-05-10" ...
 $ reCNru: num  15.9 19.2 25.2 21.3 18.6 ...
 - attr(*, ".internal.selfref")=<externalptr> 
Gecko
  • 354
  • 1
  • 10
  • 2
    Perhaps https://stackoverflow.com/questions/4350440/split-data-frame-string-column-into-multiple-columns?rq=1 helps you? – Martin Gal May 13 '20 at 16:28
  • That helps, thank you, did not see this question before. – Gecko May 13 '20 at 21:28
  • 1
    Does this answer your question? [Split data frame string column into multiple columns](https://stackoverflow.com/questions/4350440/split-data-frame-string-column-into-multiple-columns) – Martin Gal May 13 '20 at 21:43

1 Answers1

0

First, we need some sample data, taken from what you posted:

dataset <- data.frame(reCNru = c(15.9, 19.2, 25.2, 21.3),
                      rn = c("oberschlumpf.2020-05-13", "oberschlumpf.2020-05-12", 
                             "oberschlumpf.2020-05-11", "oberschlumpf.2020-05-10"), 
                      stringsAsFactors = FALSE)

Then we apply the following code in Base R:

newdataset <- setNames(do.call(rbind.data.frame, strsplit(unlist(dataset$rn), '\\.')), 
         c('rn1', 'rn2')) 
newdataset$reCNru <- dataset$reCNru

Perhaps it is interesting to see the solution given by the tidyverse:

dataset %>% tidyr::separate(col = rn, into = c("rn1","rn2"), sep = "\\.")

You will have:

reCNru          rn1        rn2
1   15.9 oberschlumpf 2020-05-13
2   19.2 oberschlumpf 2020-05-12
3   25.2 oberschlumpf 2020-05-11
4   21.3 oberschlumpf 2020-05-10

Be aware that the separator is not just "." but an expression which represents the dot.

Hope it helps.

Manu
  • 1,070
  • 10
  • 27
  • 1
    Separate takes the argument `sep` as a regex expression, but `.` means "any character" so you scape it using `\\.` – Alexis May 14 '20 at 01:55