【问题标题】:R - split data frame and save to different filesR - 拆分数据框并保存到不同的文件
【发布时间】:2015-10-30 00:22:43
【问题描述】:

我有一个包含多个地点的月度温度数据的数据框:

    > df4[1:36,]
       location    variable cut month year freq
1    Adamantina temperature  10   Jan 1981 21.0
646  Adamantina temperature  10   Feb 1981 20.5
1291 Adamantina temperature  10   Mar 1981 21.5
1936 Adamantina temperature  10   Apr 1981 21.5
2581 Adamantina temperature  10   May 1981 24.0
3226 Adamantina temperature  10   Jun 1981 21.5
3871 Adamantina temperature  10   Jul 1981 22.5
4516 Adamantina temperature  10   Aug 1981 23.5
5161 Adamantina temperature  10   Sep 1981 19.5
5806 Adamantina temperature  10   Oct 1981 21.5
6451 Adamantina temperature  10   Nov 1981 23.0
7096 Adamantina temperature  10   Dec 1981 19.0
2        Adolfo temperature  10   Jan 1981 24.0
647      Adolfo temperature  10   Feb 1981 20.0
1292     Adolfo temperature  10   Mar 1981 24.0
1937     Adolfo temperature  10   Apr 1981 23.0
2582     Adolfo temperature  10   May 1981 18.0
3227     Adolfo temperature  10   Jun 1981 21.0
3872     Adolfo temperature  10   Jul 1981 22.0
4517     Adolfo temperature  10   Aug 1981 19.0
5162     Adolfo temperature  10   Sep 1981 19.0
5807     Adolfo temperature  10   Oct 1981 24.0
6452     Adolfo temperature  10   Nov 1981 24.0
7097     Adolfo temperature  10   Dec 1981 24.0
3         Aguai temperature  10   Jan 1981 24.0
648       Aguai temperature  10   Feb 1981 20.0
1293      Aguai temperature  10   Mar 1981 22.0
1938      Aguai temperature  10   Apr 1981 20.0
2583      Aguai temperature  10   May 1981 21.5
3228      Aguai temperature  10   Jun 1981 20.5
3873      Aguai temperature  10   Jul 1981 24.0
4518      Aguai temperature  10   Aug 1981 23.5
5163      Aguai temperature  10   Sep 1981 18.5
5808      Aguai temperature  10   Oct 1981 21.0
6453      Aguai temperature  10   Nov 1981 22.0
7098      Aguai temperature  10   Dec 1981 23.5

我需要做的是按位置以编程方式拆分此数据帧,并为每个位置创建一个 .Rdata 文件。

在上面的示例中,我将拥有三个不同的文件 - Adamantina.Rdata、Adolfo.Rdata 和 Aguai.Rdata - 包含所有列,但仅包含与这些位置对应的行。

它需要高效且程序化,因为在我的实际数据中,我有大约 700 个不同的位置以及每个位置大约 50 年的数据。

提前致谢。

【问题讨论】:

  • 你试过什么?你被困在哪里了? for (loc in unique(df4$location)) save(df4[df4$location == loc], file = paste0(loc, ".Rdata")) 应该可以工作。对于边际速度增益(对于这个简单的操作),您可以改用dplyr::dodata.table,但何必呢?'
  • @Gregor 我在尝试您的建议时收到错误消息Error in save(df4[df4$location == loc], file = paste0("/disk1/Project/Shiny/data/", : object ‘df4[df4$location == loc]’ not found
  • 我忘了逗号,df4[df4$location == loc, ]

标签: r file split dataframe


【解决方案1】:

这是从以前的答案中借用的,但我不相信你想要的答案。

首先,正如他们所建议的,您想要拆分数据集。

splitData <- split(df4, df4$location)

现在,要一一浏览此列表,保存您的数据集,这可以通过删除名称来完成:

 allNames <- names(splitData)
 for(thisName in allNames){
     saveName = paste0(thisName, '.Rdata')
     saveRDS(splitData[[thisName]], file = saveName)
}

【讨论】:

  • 感谢您的建议!它适用于saveRDS,但当我尝试仅使用save 时会返回错误。它说Error in save(splitData[[thisName]], file = saveName) : object ‘splitData[[thisName]]’ not found
  • @thiagoveloso:您可以使用save 来执行此操作,但出于一些原因,我个人更喜欢saveRDS,尤其是考虑到您的目标是拥有单独的.Rdata 文件.如果你真的想要,你可以像.GlobalEnv[[thisName]] &lt;- splitData[[thisName]]; eval(substitute(save(NAME, FILENAME), list(NAME = as.name(thisName), FILENAME = saveName) ) ) 这样疯狂。但这真的是想把save() 塞进去,我绝对不推荐它。不过可能有更好的方法来使用save
【解决方案2】:

要拆分数据框,请使用split(df4, df4$location)。它将创建名为AdamantinaAdolfoAguai 等的数据帧。

要将这些新数据帧保存到locations.RData 文件中,请使用save(Adamantina, Adolfo, Aguai, file="locations.RData")save.image(file="filename.RData") 会将当前 R 会话中的所有内容保存到 filename.RData 文件中。

您可以阅读有关savesave.image here 的更多信息。

编辑:

如果拆分数量太大,请使用以下方法:

locations <- split(df4, df4$location)
save(locations, "locations.RData")

locations.RData 然后将加载为列表。

【讨论】:

  • 上帝的建议,但想法是只保存单个数据帧,最好不要手动指定它们的名称(几乎有 700 个)。
猜你喜欢
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 2020-12-04
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
相关资源
最近更新 更多