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.