2

I receive a named list of data frames using an R API to a data vendor. Below is how I would struct a similar structure.

spec1 <- data.frame(speed = 100, wt = 5, price = 10)
spec2 <- data.frame(speed = 1000, wt = 1, price = 100)
spec3 <- data.frame(speed = 1, wt = 50, price = 5)
mylist <- list(car = spec1,exotic = spec2, truck = spec3)

My goal is to turn this into a flat data frame like below so that I can export it into an SQL database.

car_type   speed     wt     price
--------   ------    ----   -----
car        100       5      10        
exotic     1000      1      100 
truck      1         50     5

My problem is that the identifier for "car_type" is stored as the name of the data frame in the list, and I don't know how to pass it to a repeat function to populate the table.

For example I use the following to get the car_type from the names of the items in the list

car_type <- sapply(seq_along(mylist), function(x) names(mylist)[x])

And then I use the following to repeat the a car_type (substituted by "X") in the underlying tables and then append.

category <- lapply(mylist, function(y) rep("X" ,dim(y)[1]))
mylist <- lapply(seq_along(mylist), function(x) 

However, I can't seem to manage to pass the names found in car_type back into create a new category column in my dataset, which will persist when it's flattened.

3 Answers 3

1

you can use either one of those two:

  dplyr::bind_rows(mylist, .id = "car_type")
  data.table::rbindlist(mylist, idcol = "car_type")

both output:

      car_type speed wt price
1:      car   100  5    10
2:   exotic  1000  1   100
3:    truck     1 50     5
Sign up to request clarification or add additional context in comments.

1 Comment

LOL. Brilliant and so easy. Spent so many hours trying to figure this out. Thanks so much @ friep
0

Or a base R option would be with Map to create the 'car_type' from the names of 'mylist' and then rbind the list elements

do.call(rbind, unname(Map(cbind, car_type = names(mylist), mylist)))
#   car_type speed wt price
#1      car   100  5    10
#2   exotic  1000  1   100
#3    truck     1 50     5

Comments

0

Maybe more readable:

library(magrittr)
mylist %>% do.call(rbind,.) %>% transform(car_type = rownames(.)) %>% set_rownames(NULL)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.