讓我們列個清單 lis
chicago = data.frame('city' = rep('chicago'), 'year' = c(2018,2019,2020), 'population' = c(100, 105, 110))
paris = data.frame('city' = rep('paris'), 'year' = c(2018,2019,2020), 'population' = c(200, 205, 210))
berlin = data.frame('city' = rep('berlin'), 'year' = c(2018,2019,2020), 'population' = c(300, 305, 310))
bangalore = data.frame('city' = rep('bangalore'), 'year' = c(2018,2019,2020), 'population' = c(400, 405, 410))
lis = list(chicago = chicago, paris = paris, berlin = berlin, bangalore = bangalore)
現在我有一個df包含每個最新資料的新資料city,
df = data.frame('city' = c('chicago', 'paris', 'berlin', 'bangalore'), 'year' = rep(2021), 'population' = c(115, 215, 315, 415))
我想補充的每一行df,以lis根據city。
我這樣做,
#convert to datframe
lis = dplyr::bind_rows(lis)
#rbind
lis = rbind(lis, df)
#again convert to list
lis = split(lis, lis$city)
這對于大型資料集效率低下。對于大型資料集,它們是否有任何有效的替代方案?
謝謝你。
編輯
Unit: seconds
expr min lq mean median uq max neval
ac() 22.43719 23.17452 27.85401 24.80335 25.62127 43.23373 5
該串列包含2239資料幀,每個資料幀的維度是310x15. 這些資料幀中的每一個每天都在增長。
uj5u.com熱心網友回復:
我們可以使用基于 的名稱imap回圈遍歷list和filter'df' 以list在每個list元素中附加行
library(dplyr)
library(purrr)
lis2 <- imap(lis, ~ .x %>%
bind_rows(df %>%
filter(city == .y)))
-輸出
> lis2
$chicago
city year population
1 chicago 2018 100
2 chicago 2019 105
3 chicago 2020 110
4 chicago 2021 115
$paris
city year population
1 paris 2018 200
2 paris 2019 205
3 paris 2020 210
4 paris 2021 215
$berlin
city year population
1 berlin 2018 300
2 berlin 2019 305
3 berlin 2020 310
4 berlin 2021 315
$bangalore
city year population
1 bangalore 2018 400
2 bangalore 2019 405
3 bangalore 2020 410
4 bangalore 2021 415
或base R與Map和一起使用rbind
Map(function(x, nm) rbind(x, df[df$city == nm,]), lis, names(lis))
或使用rbindlist從data.table
library(data.table)
rbindlist(c(lis, list(df)))[, .(split(.SD, city))]$V1
或者稍微更有效的,將與 split
Map(rbind, lis, split(df, df$city)[names(lis)])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/340132.html
