【问题标题】:List to data.frame in r列表到 r 中的 data.frame
【发布时间】:2018-07-03 11:21:25
【问题描述】:

有没有更好的方法将巨大的列表(超过 100k)转换为 data.frame。

for (i in 1:length(google_20180623_list)) {

    if (google_20180623_list[[i]]$status != "OK") next

    name <- google_20180623_list[[i]]$result$name
    formatted_address <- google_20180623_list[[i]]$result$formatted_address
    place_id <- google_20180623_list[[i]]$result$place_id
    geom <- data.frame(google_20180623_list[[i]]$result$geometry$location)
    types <- google_20180623_list[[i]]$result$types
    types <- paste(types, collapse = "/")

    oa <- google_20180623_list[[i]]$result$opening_hours$weekday_text
    if (is.null(oa)){
        oa <- NA

    } else {
        oa <- as.data.frame(oa)
        oa <- oa %>% separate(oa,into=c("days", "opening"), sep=": ", remove=T)
        oa <- oa %>% spread(key = "days", value = "opening")
    }      
    rating <- google_20180623_list[[i]]$result$rating
    if (is.null(rating)) {rating<-NA}

    price_level <- google_20180623_list[[i]]$result$price_level
    if (is.null(price_level)) {price_level<-NA}

    phone <- google_20180623_list[[i]]$result$international_phone_number
    if (is.null(phone)) {phone<-NA}

    website <- google_20180623_list[[i]]$result$website
    if (is.null(website)) {website<-NA}
    cb<-cbind(place_id
                ,name
                ,formatted_address
                ,geom
                ,phone
                ,website
                ,rating
                ,price_level
                ,oa
                ,types
      )

    google_20180623_list_unlist<-rbindlist(list(google_20180623_list_unlist,cb),fill = T)

}

这段代码非常慢,需要超过 24 小时。任何的想法?

数据来自google place details api。

感谢您的帮助。

【问题讨论】:

  • 也显示列表的 sn-p。

标签: r performance list dataframe


【解决方案1】:

我认为用另一种编程语言来完成这项任务会更有效率。尝试将其转换为文件,然后用 R 读取它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多