Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.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_Time Series_Row_Aggregate - Fatal编程技术网

R 如何按组对时间序列数据行求和?

R 如何按组对时间序列数据行求和?,r,time-series,row,aggregate,R,Time Series,Row,Aggregate,我的数据集如下所示: block <- c(1,1,2,2,3,3,4,4) treatment <- c(1,1,2,2,1,1,2,2) type <- c("adult1","adult2","adult1","adult2","adult1","adult2","adult1","adult2") t1 <- c(1,1,2,2,3,3,4,4) t2 <- c(1,1,2,2,3,3,4,4) t100 <- c(1,1,2,2,3,3,4,4) d

我的数据集如下所示:

block <- c(1,1,2,2,3,3,4,4)
treatment <- c(1,1,2,2,1,1,2,2)
type <- c("adult1","adult2","adult1","adult2","adult1","adult2","adult1","adult2")
t1 <- c(1,1,2,2,3,3,4,4)
t2 <- c(1,1,2,2,3,3,4,4)
t100 <- c(1,1,2,2,3,3,4,4)
df <- data.frame(block,treatment, type,t1,t2,t100)

我收到一条错误消息,说“参数必须具有相同的长度”。

对于
聚合
,您可以使用公式将
t1:t100
相加,并按
治疗进行分组:

df_final = aggregate(cbind(t1, t2, t100) ~ block + treatment, data = df, sum)
df_final$type1 = "adult"
结果:

  block treatment t1 t2 t100 type1
1     1         1  2  2    2 adult
2     3         1  6  6    6 adult
3     2         2  4  4    4 adult
4     4         2  8  8    8 adult
# A tibble: 4 x 6
# Groups:   block [4]
  block treatment    t1    t2  t100 type1
  <dbl>     <dbl> <dbl> <dbl> <dbl> <chr>
1     1         1     2     2     2 adult
2     2         2     4     4     4 adult
3     3         1     6     6     6 adult
4     4         2     8     8     8 adult
   block treatment t1 t2 t100
1:     1         1  2  2    2
2:     2         2  4  4    4
3:     3         1  6  6    6
4:     4         2  8  8    8
或者您可以使用
dplyr

library(dplyr)

df %>%
  group_by(block, treatment) %>%
  summarize_at(vars(t1:t100), sum) %>%
  mutate(type1 = "adult")

结果:

  block treatment t1 t2 t100 type1
1     1         1  2  2    2 adult
2     3         1  6  6    6 adult
3     2         2  4  4    4 adult
4     4         2  8  8    8 adult
# A tibble: 4 x 6
# Groups:   block [4]
  block treatment    t1    t2  t100 type1
  <dbl>     <dbl> <dbl> <dbl> <dbl> <chr>
1     1         1     2     2     2 adult
2     2         2     4     4     4 adult
3     3         1     6     6     6 adult
4     4         2     8     8     8 adult
   block treatment t1 t2 t100
1:     1         1  2  2    2
2:     2         2  4  4    4
3:     3         1  6  6    6
4:     4         2  8  8    8
结果:

  block treatment t1 t2 t100 type1
1     1         1  2  2    2 adult
2     3         1  6  6    6 adult
3     2         2  4  4    4 adult
4     4         2  8  8    8 adult
# A tibble: 4 x 6
# Groups:   block [4]
  block treatment    t1    t2  t100 type1
  <dbl>     <dbl> <dbl> <dbl> <dbl> <chr>
1     1         1     2     2     2 adult
2     2         2     4     4     4 adult
3     3         1     6     6     6 adult
4     4         2     8     8     8 adult
   block treatment t1 t2 t100
1:     1         1  2  2    2
2:     2         2  4  4    4
3:     3         1  6  6    6
4:     4         2  8  8    8

使用
aggregate
可以使用公式对
t1:t100
进行汇总,并按
block
治疗进行分组

df_final = aggregate(cbind(t1, t2, t100) ~ block + treatment, data = df, sum)
df_final$type1 = "adult"
结果:

  block treatment t1 t2 t100 type1
1     1         1  2  2    2 adult
2     3         1  6  6    6 adult
3     2         2  4  4    4 adult
4     4         2  8  8    8 adult
# A tibble: 4 x 6
# Groups:   block [4]
  block treatment    t1    t2  t100 type1
  <dbl>     <dbl> <dbl> <dbl> <dbl> <chr>
1     1         1     2     2     2 adult
2     2         2     4     4     4 adult
3     3         1     6     6     6 adult
4     4         2     8     8     8 adult
   block treatment t1 t2 t100
1:     1         1  2  2    2
2:     2         2  4  4    4
3:     3         1  6  6    6
4:     4         2  8  8    8
或者您可以使用
dplyr

library(dplyr)

df %>%
  group_by(block, treatment) %>%
  summarize_at(vars(t1:t100), sum) %>%
  mutate(type1 = "adult")

结果:

  block treatment t1 t2 t100 type1
1     1         1  2  2    2 adult
2     3         1  6  6    6 adult
3     2         2  4  4    4 adult
4     4         2  8  8    8 adult
# A tibble: 4 x 6
# Groups:   block [4]
  block treatment    t1    t2  t100 type1
  <dbl>     <dbl> <dbl> <dbl> <dbl> <chr>
1     1         1     2     2     2 adult
2     2         2     4     4     4 adult
3     3         1     6     6     6 adult
4     4         2     8     8     8 adult
   block treatment t1 t2 t100
1:     1         1  2  2    2
2:     2         2  4  4    4
3:     3         1  6  6    6
4:     4         2  8  8    8
结果:

  block treatment t1 t2 t100 type1
1     1         1  2  2    2 adult
2     3         1  6  6    6 adult
3     2         2  4  4    4 adult
4     4         2  8  8    8 adult
# A tibble: 4 x 6
# Groups:   block [4]
  block treatment    t1    t2  t100 type1
  <dbl>     <dbl> <dbl> <dbl> <dbl> <chr>
1     1         1     2     2     2 adult
2     2         2     4     4     4 adult
3     3         1     6     6     6 adult
4     4         2     8     8     8 adult
   block treatment t1 t2 t100
1:     1         1  2  2    2
2:     2         2  4  4    4
3:     3         1  6  6    6
4:     4         2  8  8    8
基R中的溶液:

df <- cbind.data.frame(
    aggregate(cbind(t1, t2, t100) ~ block + treatment, data = df, FUN = sum),
    type = "adult");
#  block treatment t1 t2 t100  type
#1     1         1  2  2    2 adult
#2     3         1  6  6    6 adult
#3     2         2  4  4    4 adult
#4     4         2  8  8    8 adult
基R中的溶液:

df <- cbind.data.frame(
    aggregate(cbind(t1, t2, t100) ~ block + treatment, data = df, FUN = sum),
    type = "adult");
#  block treatment t1 t2 t100  type
#1     1         1  2  2    2 adult
#2     3         1  6  6    6 adult
#3     2         2  4  4    4 adult
#4     4         2  8  8    8 adult

比我快;-)不错的一个@useR。我的数据集中有几个col。我试着这样做:
df_final=aggregate(cbind(df[,3:5])~block+treatment,data=df,sum)
我得到一个错误,表示类型列表无效。有什么建议吗?@PythonDabble在这种情况下,我建议使用
dplyr
方法,或者使用
as.formula
paste0
的变量名构造一个公式,并将其输入到
aggregate
中。我认为
aggregate
中的
cbind
语法不支持列索引。我尝试在输入
时使用dplyr(vars(df[4:6])
但我得到一个错误,该错误表示必须解析为整数列位置,而不是list@useR你可能会对我添加的
microbenchmark
比较感兴趣。我总是惊讶于
数据的速度有多快。table
可以。快告诉我;-)一个不错的@useR。我的数据集中有几个col。我试着这样做:
df_final=aggregate(cbind(df[,3:5])~block+treatment,data=df,sum)
我得到一个错误,表示类型列表无效。有什么建议吗?@PythonDabble在这种情况下,我建议使用
dplyr
方法,或者使用
as.formula
paste0
的变量名构造一个公式,并将其输入到
aggregate
中。我认为
aggregate
中的
cbind
语法不支持列索引。我尝试在输入
时使用dplyr(vars(df[4:6])
但我得到一个错误,该错误表示必须解析为整数列位置,而不是list@useR您可能对我添加的
microbenchmark
比较感兴趣。我总是对
data.table
的速度感到惊讶。@useR您是对的。不知道这是如何工作的。现在更正,并且(接近-)与您的相同。在我的辩护中,您最初只有
dplyr
解决方案,后来添加了基本R解决方案。@用户您是对的。不知道这是如何工作的。现在更正,与您的(几乎)相同。在我的辩护中,您最初只有
dplyr
解决方案,后来添加了基本R解决方案。