Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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 每n行跳转平均列_R_Split_Average_Intervals - Fatal编程技术网

R 每n行跳转平均列

R 每n行跳转平均列,r,split,average,intervals,R,Split,Average,Intervals,请帮我做这件事。。 所以我有32年的每日观察(数据框架)。(因此总计约11659行:缺少一些行) 我想每365个间隔计算每列的平均值(即,32年期间每01年1月,32年期间每02年1月,等等) 因此,输出将总共有365行,每行以365个间隔平均32行。 有什么建议吗?我发现了类似的情况,尝试了他们的解决方案,并做了一些修改,但输出不正确。尤其是我不理解下面的sapply部分 df <-data.frame(x=c(1:10000),y=c(1:10000)) byapply <- f

请帮我做这件事。。 所以我有32年的每日观察(数据框架)。(因此总计约11659行:缺少一些行) 我想每365个间隔计算每列的平均值(即,32年期间每01年1月,32年期间每02年1月,等等)

因此,输出将总共有365行,每行以365个间隔平均32行。 有什么建议吗?我发现了类似的情况,尝试了他们的解决方案,并做了一些修改,但输出不正确。尤其是我不理解下面的sapply部分

df <-data.frame(x=c(1:10000),y=c(1:10000))
byapply <- function(x, by, fun, ...)
{
# Create index list
if (length(by) == 1)
{
    nr <- nrow(x)
    split.index <- rep(1:ceiling(nr / by), each = by, length.out = nr)
} else 
{
    nr <- length(by)
    split.index <- by
}
index.list <- split(seq(from = 1, to = nr), split.index)

# Pass index list to fun using sapply() and return object #this is where I am lost
sapply(index.list, function(i)
        {
            do.call(fun, list(x[, i], ...))
        })
}

df虽然可能有一个特殊的函数,它完全满足您的需要,但这里有一个使用
ave
的解决方案:

set.seed(1)
dates = seq(from=as.Date("1970-01-01"), as.Date("2000-01-01"), by="day")
df <- data.frame(val1=runif(length(dates)), 
                 val2=rchisq(length(dates), 10))
day <- format(dates, "%j") # day of year (1:366)

df <- cbind(df, setNames(as.data.frame(sapply(df, function(x) {
  ave(x, day) # calculate mean by day for df$val1 and df$val2
})), paste0(names(df), "_mean")))

head(df[1:365, 3:4], 3)
#   val1_mean val2_mean
# 1 0.5317151 10.485001
# 2 0.5555664 10.490968
# 3 0.6428217 10.763027
set.seed(1)
日期=序号(起始日期=截止日期(“1970-01-01”)、截止日期(“2000-01-01”)、截止日期

df如何使用
plyr
软件包:

require(plyr)    # for aggregating data

require(plyr)    # for aggregating data

series<-data.frame(date=as.Date("1964-01-01")+(1:100000),
                   obs=runif(10000),
                   obs2=runif(10000),
                   obs3=runif(10000))

ddply(series,                     # run on series df
      .(DOY=format(date,"%j")),   # group by string of day and month (call col DOY)
      summarise,                  # tell the function to summarise by group (day of year)
      daymean=mean(obs),          # calculate the mean
      daymean2=mean(obs2),        # calculate the mean
      daymean3=mean(obs3)         # calculate the mean
)

#    DOY   daymean  daymean2  daymean3
#1   001 0.4957763 0.4882559 0.4944281
#2   002 0.5184197 0.4970996 0.4720893
#3   003 0.5192313 0.5185357 0.4878891
#4   004 0.4787227 0.5150596 0.5317068
#5   005 0.4972933 0.5065012 0.4956527
#6   006 0.5112484 0.5276013 0.4785681
#...
require(plyr)#用于聚合数据
需要(plyr)#用于聚合数据

seriesindex.list如果您使用的是天气观测,如何处理闰年?最好为您的数据生成一些新的列,实际上是日期,一年中的某一天。然后您可以使用应用函数。是的,这也是我遇到的问题。我需要重新组合数据,谢谢您的评论。