0

I have a dataframe with a column which contains lists of two numbers. I'd like to break it into 2 columns. I found a convoluted solution, but was hoping someone could improve on it.

My solution:

column=list(c(1,2),c(3,4))

cols = list(1,length(column[[1]]))

# given a list, return the ith element
f = function(l, i) {
  return(l[i])
}
# apply function f to each column 
g = function(i) {
  sapply(column, f, i)
}
# for each column ...
sapply(cols, g)

This gives the desired output:

     [,1] [,2]
[1,]    1    2
[2,]    3    4
Evan Rosica
  • 1,182
  • 1
  • 12
  • 22

0 Answers0