2

I have a data.frame and I want to split one of its columns to two based on a regular expression. More specifically the strings have a suffix in parentheses that needs to be extracted to a column of its own.

So e.g. I want to get from here:

dfInit <- data.frame(VAR = paste0(c(1:10),"(",c("A","B"),")"))

to here:

dfFinal <- data.frame(VAR1 = c(1:10), VAR2 = c("A","B"))
chiwangc
  • 3,566
  • 16
  • 26
  • 32
Antti
  • 1,263
  • 2
  • 16
  • 28

5 Answers5

6

1) gsubfn::read.pattern read.pattern in the gsubfn package can do that. The matches to the parenthesized portions of the regular rexpression are regarded as the fields:

library(gsubfn)
read.pattern(text = as.character(dfInit$VAR), pattern = "(.*)[(](.*)[)]$")

giving:

   V1 V2
1   1  A
2   2  B
3   3  A
4   4  B
5   5  A
6   6  B
7   7  A
8   8  B
9   9  A
10 10  B

2) sub Another way is to use sub:

data.frame(V1=sub("\\(.*", "", dfInit$VAR), V2=sub(".*\\((.)\\)$", "\\1", dfInit$VAR))

giving the same result.

3) read.table This solution does not use a regular expression:

read.table(text = as.character(dfInit$VAR), sep = "(", comment = ")")

giving the same result.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
3

You could also use extract from tidyr

library(tidyr)
extract(dfInit, VAR, c("VAR1", "VAR2"), "(\\d+).([[:alpha:]]+).", convert=TRUE) # edited and added `convert=TRUE` as per @aosmith's comments.



#    VAR1 VAR2
#1     1    A
#2     2    B
#3     3    A
#4     4    B
#5     5    A
#6     6    B
#7     7    A
#8     8    B
#9     9    A
#10   10    B
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Setting `convert` to `TRUE` in `extract` avoids the need for `mutate`, although `VAR2` is then converted to a factor. – aosmith Oct 15 '14 at 14:40
1

See Split column at delimiter in data frame

dfFinal <- within(dfInit, VAR<-data.frame(do.call('rbind', strsplit(as.character(VAR), '[[:punct:]]'))))

> dfFinal
   VAR.X1 VAR.X2
1       1      A
2       2      B
3       3      A
4       4      B
5       5      A
6       6      B
7       7      A
8       8      B
9       9      A
10     10      B
Community
  • 1
  • 1
rgunning
  • 568
  • 2
  • 16
1

You can also use cSplit from splitstackshape.

library(splitstackshape)
cSplit(dfInit, "VAR", "[()]", fixed=FALSE)
#    VAR_1 VAR_2
# 1:     1     A
# 2:     2     B
# 3:     3     A
# 4:     4     B
# 5:     5     A
# 6:     6     B
# 7:     7     A
# 8:     8     B
# 9:     9     A
#10:    10     B
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
1

An approach with regmatches and gregexpr:

as.data.frame(do.call(rbind, regmatches(dfInit$VAR, gregexpr("\\w+", dfInit$VAR))))
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168