【问题标题】:Saving dataframes stored in a list to individual files in R将存储在列表中的数据帧保存到 R 中的单个文件中
【发布时间】:2020-01-21 09:35:57
【问题描述】:

我有一个很大的列表,merged_fin,包含 39 个数据帧。数据集如下所示:

> merged_fin[[1]]
    sourceid dstid       speed
1        177     1 0.010604494
2         46     4 0.010794178
3        100     7 0.007286781

> merged_fin[[2]]
    sourceid dstid       speed
1        721    12 0.013830787
2         23    15 0.016334978
3        274    16 0.015247266
...

我想将该列表中的每个数据集保存到我的工作目录中自己的.rds 文件中。

  • 正在尝试:
for (i in 1:length(merged_fin)){
saveRDS(merged_fin[[i]])}

或者

saveRDS(merged_fin[[1]])

我收到Error in saveRDS(merged_fin[[i]]) : 'file' must be non-empty string

  • 正在尝试:
lapply(names(merged_fin), function(i)
  saveRDS(merged_fin[[i]], paste0(i, '.rds')))

我收到list(),但没有文件保存到我的工作目录中。

注意事项: (1)names(merged_fin)输出NULL; (2) 我最初将 merged_fin 编码为一个空列表 (merged fin <- list()),然后用我从不同文件夹中读取的合并数据集填充它。

  • 问题是否在于我引用列表元素的方式?
  • 是因为最初定义merged_fin 的方式吗?

感谢您的帮助。

解决方案

就我而言,这只是一个命名列表元素的问题,这是 meenaparam 建议的。我有一个包含正确排序的城市名称的向量,称为cities。我刚刚做了names(merged_fin) <- cities,这足以成功运行

lapply(names(merged_fin), function(i)
   saveRDS(merged_fin[[i]], paste0(i, '.rds')))

【问题讨论】:

    标签: r list save


    【解决方案1】:

    继上一个答案之后,这里有一个示例,说明如何在 merged_fin 列表中分配并获取数据帧的名称。请注意,如果您的数据框还没有单独的名称,您也可以使用 names(merged_fin) <- c("name1", "name2") 等简单地分配它们。

    df1 <- read.table(h=T, text="
                         sourceid dstid       speed
    1        177     1 0.010604494
    2         46     4 0.010794178
    3        100     7 0.007286781")
    
    df2 <- read.table(h=T, text="
                          sourceid dstid       speed
    1        721    12 0.013830787
    2         23    15 0.016334978
    3        274    16 0.015247266")
    
    # make a list of dataframes
    merged_fin <- list(df1, df2)
    
    # see that the names of merged_fin are currently set to NULL
    names(merged_fin)
    #> NULL
    
    
    # get the names of all the list-type objects in the workspace that contain the string "df" - we do this because dataframes are stored as lists
    names_of_dataframes <- ls.str(mode = "list", pattern = "df")
    names_of_dataframes
    #> df1 : 'data.frame':  3 obs. of  3 variables:
    #>  $ sourceid: int  177 46 100
    #>  $ dstid   : int  1 4 7
    #>  $ speed   : num  0.0106 0.01079 0.00729
    #> df2 : 'data.frame':  3 obs. of  3 variables:
    #>  $ sourceid: int  721 23 274
    #>  $ dstid   : int  12 15 16
    #>  $ speed   : num  0.0138 0.0163 0.0152
    
    
    # assign the dataframe names back to our list of dataframes
    names(merged_fin) <- names_of_dataframes
    names(merged_fin)
    #> [1] "df1" "df2"
    
    
    # now we can write out the dataframes to files as each dataframe has a name
    lapply(names(merged_fin), function(i)
        saveRDS(merged_fin[[i]], paste0("~/Desktop/", i, '.rds')))
    #> [[1]]
    #> NULL
    #> 
    #> [[2]]
    #> NULL
    

    reprex package (v0.3.0) 于 2020-01-21 创建

    【讨论】:

    • 就我而言,只需将名称添加到列表元素就足以使lapply(names(merged_fin), function(i) saveRDS(merged_fin[[i]], paste0(i, '.rds'))) 工作。谢谢。
    猜你喜欢
    • 2017-09-26
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 2022-06-29
    • 2016-05-19
    • 2021-04-04
    相关资源
    最近更新 更多