Luongjames
Luongjames

Reputation: 55

R Passing Name of List Objects to List Objects Themselves

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.

Upvotes: 2

Views: 236

Answers (3)

moodymudskipper
moodymudskipper

Reputation: 47350

Maybe more readable:

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

Upvotes: 0

akrun
akrun

Reputation: 887881

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

Upvotes: 0

pr140
pr140

Reputation: 321

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

Upvotes: 1

Related Questions