R-分割数据框并保存到其他文件 [英] R - split data frame and save to different files

查看:174
本文介绍了R-分割数据框并保存到其他文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中包含多个位置的每月温度数据:

I have a data frame with monthly temperature data for several locations:

    > 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文件。

What I need to do is to programmatically split this data frame by location and create a .Rdata file for every location.

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

In the example above, I would have three different files - Adamantina.Rdata, Adolfo.Rdata and Aguai.Rdata - containing all the columns but only the rows corresponding to those locations.

高效和程序化,因为在我的实际数据中,我有大约700个不同的位置,每个位置都有大约50年的数据。

It needs to be efficient and programmatic, because in my actual data I have about 700 different locations and about 50 years of data for every location.

先谢谢了。

推荐答案

这是从先前的答案中借来的,但是我不相信您想要的答案。

This is borrowing from a previous answer, but I don't believe that answer does you want.

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

First, as they suggest, you want to split up your data set.

splitData<-split(df4,df4 $ location)

现在,要遍历此列表,并保存您的数据集,可以通过删除名称来完成:

Now, to go through this list and one by one, save your datasetset, this can be done with by pulling off the names:

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

这篇关于R-分割数据框并保存到其他文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆