0

Here is an example dataframe:

x <- list(a = c("1", "2", "2, 3"), b = c("2", "3", "5, 6"), id = c("a1", "a2", "a3"))
x1 <- as.data.frame(x)

str(x1)

'data.frame':   3 obs. of  3 variables:
$ a : Factor w/ 3 levels "1","2","2, 3": 1 2 3
$ b : Factor w/ 3 levels "2","3","5, 6": 1 2 3
$ id: Factor w/ 3 levels "a1","a2","a3": 1 2 3

x1

a     b     id
1     2     a1
2     3     a2
2, 3  5, 6  a3

you see that x1$a and x1$b have two values on position: "2, 3" and "5, 6", but on x1$id there is just one value.

My question is if you know a way to split the two values, make a forth value with the same $id value. So that it looks like this:

str(x1)

'data.frame':   4 obs. of  3 variables:
$ a : Factor w/ 4 levels "1","2","2","3": 1 2 3 4
$ b : Factor w/ 4 levels "2","3","5","6": 1 2 3 4
$ id: Factor w/ 3 levels "a1","a2","a3", "a3": 1 2 3

x1

a     b     id
1     2     a1
2     3     a2
2     5     a3
3     6     a3

Edit: $a and $b always have the same amount of variables

Edit2: Thanks to Ronak Shah it worked

-> cSplit(x, c("a", "b"), ",", "long")
Arthur Pennt
  • 155
  • 1
  • 14

0 Answers0