Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 累积总和和数据组织_R_Csv_Merge_Cumulative Sum - Fatal编程技术网

R 累积总和和数据组织

R 累积总和和数据组织,r,csv,merge,cumulative-sum,R,Csv,Merge,Cumulative Sum,我有来自不同样本的大约40000个降雨数据值,这些数据将不断更新。csv文件的组织方式如下: NAME; YEAR; ID; VALUE Sample1; 1998; 354; 45 Sample1; 1999; 354; 23 Sample1; 2000; 354; 66 Sample1; 2001; 354; 98 Sample1; 2002; 354; 36 Sample1; 20

我有来自不同样本的大约40000个降雨数据值,这些数据将不断更新。csv文件的组织方式如下:

NAME;       YEAR;   ID;     VALUE
Sample1;    1998;   354;    45
Sample1;    1999;   354;    23
Sample1;    2000;   354;    66
Sample1;    2001;   354;    98
Sample1;    2002;   354;    36
Sample1;    2003;   354;    59
Sample1;    2004;   354;    64
Sample1;    2005;   354;    23
Sample1;    2006;   354;    69
Sample1;    2007;   354;    94
Sample1;    2008;   354;    24
Sample2;    1964;   1342;    7
Sample2;    1965;   1342;   24
Sample3;    2002;   859;    90
Sample3;    2003;   859;    93
Sample3;    2004;   859;    53
Sample3;    2005;   859;    98 
我想用R脚本做的是:创建一个新行,其中一组样本(例如,对于所有样本1,然后从所有样本2的值开始,然后从所有样本3的值开始,依此类推)基于先前的值(降雨数据的累积和)求和,例如,对于示例1,结果是一行,如本示例中的CUM_-Rainsion(例如,第一个示例:CUM_-Rainsion 1为45,然后是45+23,然后是68+66,然后是134+232,依此类推,直到样本1结束,应接管样本2的值,并重新开始该过程)

因此,我想编写一个新文件,其中包含所有具有3个以上值的行(在给定示例中,Sample2不会写入该文件,因为它只包含2个值)


在R中有没有一种简单的方法可以做到这一点?感谢您的帮助!在下面的链接下,您将找到一个包含数据的csv:

40k个观测值在基本R中应该很好

d$CUMRAIN <- unlist(by(d$VALUE, d$NAME, cumsum), use.names = FALSE)
d
#       NAME YEAR   ID VALUE CUMRAIN
# 1  Sample1 1998  354    45      45
# 2  Sample1 1999  354    23      68
# 3  Sample1 2000  354    66     134
# 4  Sample1 2001  354    98     232
# 5  Sample1 2002  354    36     268
# 6  Sample1 2003  354    59     327
# 7  Sample1 2004  354    64     391
# 8  Sample1 2005  354    23     414
# 9  Sample1 2006  354    69     483
# 10 Sample1 2007  354    94     577
# 11 Sample1 2008  354    24     601
# 12 Sample2 1964 1342     7       7
# 13 Sample2 1965 1342    24      31
# 14 Sample3 2002  859    90      90
# 15 Sample3 2003  859    93     183
# 16 Sample3 2004  859    53     236
# 17 Sample3 2005  859    98     334
后者可能是最有利的,因为它删除了因子名称

还有

library(plyr)
ddply(d, .(NAME), mutate, CUMSUM = cumsum(VALUE))     

若要为三个以上的观察值创建子集,可以使用一个简单的

t <- table(d$NAME)
ss <- d[d$NAME %in% names(t)[t > 3], ]

下面是另一种使用dplyr的方法

library(dplyr)

data %>%                                   # your data frame
  group_by(NAME) %>%                       # the grouping variable. could add more variables if necessary
  filter(n() > 3) %>%                      # n()  calculates the number of rows per group and then only those with more than 3 are filtered (selected)
  mutate(CUMRAIN = cumsum(VALUE)) %>%      # add a new column "CUMRAIN"
  write.table(., "test.csv", sep = ";")    # write the subset to a file. The "." indicates that it uses the output of the previous operations piped by %>%   
使用
%%>%%
运算符将这些操作“管道化”在一起


更新:如@Arun的回答所述,对于观测值少于3次的样本,无需计算累积降雨量,因此我们可以先使用过滤操作(在变异之前)使用包含3个以上观测值的所有样本生成子集,然后计算累积降雨量。

这里有一个使用
数据的解决方案。表
假设您的数据存储在
dat
中:

require(data.table)
ans = setDT(dat)[, crain := cumsum(VALUE[.N > 3L]), by=NAME][!is.na(crain)]
  • setDT
    将data.frame转换为data.table
  • 然后,我们按
    名称
    分组,仅当该组(
    .N
    ,内置特殊变量)的观察次数大于3L时,才计算该组的
    的累积和。我们通过引用将值分配给新列
    crain

  • 由于我们没有计算组的
    cumsum
    ,您可以将
    transform(d,CUMRAIN=ave(VALUE,NAME,FUN=cumsum))
    添加到您的方法集合中。我给您留了一些。:)谢谢你的回答!但是,如果我按照您的建议将表写入文件,则项目的顺序不正确。NAME YEAR ID VALUE CUMRAIN 1 Sample1 1998 354 45 45 2 Sample1 1999 354 23 68 3 Sample1 2000 354 66 134等等…如何将列的所有标题向右移一步,使其正确(这对于脚本中的进一步数据处理非常重要!)抱歉,格式太糟糕了:您可以在这里找到生成的.csv文件:将数据写入文件时,在
    write.table
    函数中添加参数
    row.names=FALSE
    write.table(ss, "filename", sep = ";")
    
    library(dplyr)
    
    data %>%                                   # your data frame
      group_by(NAME) %>%                       # the grouping variable. could add more variables if necessary
      filter(n() > 3) %>%                      # n()  calculates the number of rows per group and then only those with more than 3 are filtered (selected)
      mutate(CUMRAIN = cumsum(VALUE)) %>%      # add a new column "CUMRAIN"
      write.table(., "test.csv", sep = ";")    # write the subset to a file. The "." indicates that it uses the output of the previous operations piped by %>%   
    
    require(data.table)
    ans = setDT(dat)[, crain := cumsum(VALUE[.N > 3L]), by=NAME][!is.na(crain)]