简体   繁体   中英

Adding lists to rows of a dataframe in r

I have composed the following dataframe and i want to add the "values" contents as rows in the dataframe. So the values column contains lists which contain a dataframe. This is what i'm currently using without success.

name <- c("impressions_unique", "impressions_paid_unique","impressions_organic_unique")
period <- c("lifetime","lifetime","lifetime")

l1 <- list(data.frame(value = 33395))
l2 <- list(data.frame(value = 0))
l3 <- list(data.frame(value = 33395))
values <- c(l1,l2,l3)

title <- c("Lifetime Post Total Reach","Lifetime Post Paid Reach","Lifetime Post organic reach")
description <- c("Lifetime","Lifetime","Lifetime")
id <- c(125698,432566,759832)

df <- data.frame(name,period,values,title,description,id)

Now each object of the values column becomes another column in the dataframe, which i want it to be a row,that contains a list instead. Any help will be much appreciated.

Add the list column separately.

df <- data.frame(name,period,title,description,id, stringsAsFactors = FALSE)
df$values <- values

df
#                        name   period                       title description     id values
#1         impressions_unique lifetime   Lifetime Post Total Reach    Lifetime 125698  33395
#2    impressions_paid_unique lifetime    Lifetime Post Paid Reach    Lifetime 432566      0
#3 impressions_organic_unique lifetime Lifetime Post organic reach    Lifetime 759832  33395

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM