Extract list data from a column

  • A+
Category:Languages

I have a dataframe that looks really similar to this (basically a table of cities and their coordinates on a map. Note that coordinates is a list of X,Y values.

foo <- data.frame(   city = c("chicago", "new york"),    coordinate = I(list(list(10, 15), list(20, 25))),    myabbr = c("chi", "ny") )  bar <- subset(foo, select=c("city", "coordinate")) 

Right now, I can create a new table of only the city and the coordinates. I really want the X and Y values to be a separate column. This is what I tried

bar <- subset(foo, select=c("city", "coordinate[1]", "coordinate[2]")) 

I'm not sure exactly how to do this though.

 


One more option for you, use listCol_w from splitstackshape.

library(splitstackshape) listCol_w(foo, "coordinate") #       city myabbr coordinate_fl_1 coordinate_fl_2 #1:  chicago    chi              10              15 #2: new york     ny              20              25 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: