2

Sorry for the naive question but I have a dataframe like this:

  n sp   cap
1 1  a     3
2 2  b 3+2+4
3 3  c     2
4 4  d   1+5

I need to split the numbers separated by the add sign ("+") into new rows in order to the get a new dataframe like this below:

  n sp cap
1 1  a   3
2 2  b   3
3 2  b   2
4 2  b   4
5 3  c   2
6 4  d   1
7 4  d   5

How can I do that? strsplit?

thanks in advance

Diego
  • 31
  • 4

1 Answers1

4

We could use cSplit from splitstackshape

library(splitstackshape)
cSplit(df1, 'cap', sep="+", 'long')
#    n sp cap
#1: 1  a   3
#2: 2  b   3
#3: 2  b   2
#4: 2  b   4
#5: 3  c   2
#6: 4  d   1
#7: 4  d   5

Or could do this in base R. Use strsplit to split the elements of "cap" column to substrings, which returns a list (lst), Replicate the rows of dataset by the length of each list element, subset the dataset based on the new index, convert the "lst" elements to "numeric", unlist, and cbind with the modified dataset.

lst <- strsplit(as.character(df1$cap), "[+]") 

df2 <- cbind(df1[rep(1:nrow(df1), sapply(lst, length)),1:2],
              cap= unlist(lapply(lst, as.numeric)))
akrun
  • 874,273
  • 37
  • 540
  • 662