Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.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_Date - Fatal编程技术网

在R中按月获取特定月份的平均值

在R中按月获取特定月份的平均值,r,date,R,Date,我有这样的数据 logMosqm2 Date [1,] -4.0000000 10296 [2,] -4.0000000 10313 [3,] -4.0000000 10342 [4,] -4.0000000 10388 [5,] -0.9592633 10526 [6,] -4.0000000 10572 只有四列(类似于第一列)和更多行 我想得到logMosm2(以及其他类似变量)的月平均值,但仅限于2004年、2005年和2006年的月平均值 关于日期:这些来自 yearf

我有这样的数据

      logMosqm2  Date
[1,] -4.0000000 10296
[2,] -4.0000000 10313
[3,] -4.0000000 10342
[4,] -4.0000000 10388
[5,] -0.9592633 10526
[6,] -4.0000000 10572
只有四列(类似于第一列)和更多行

我想得到logMosm2(以及其他类似变量)的月平均值,但仅限于2004年、2005年和2006年的月平均值

关于日期:这些来自

yearfish <- cbind(logMosqm2, Date)
表中的日期是自1970年1月1日起的天数

编辑:我在
ZOO
包中找到了
yearmon
函数。现在我有一个变量yearmon:

[1] "Mar 1998" "Mar 1998" "Apr 1998" "Jun 1998"
 [5] "Oct 1998" "Dec 1998" "Apr 1999" "Nov 1999"
 [9] "Feb 2000" "Feb 2000" "Mar 2000" "Apr 2000"
[13] "May 2000" "Jun 2000" "Oct 2000" "Dec 2000"
[17] "Mar 2001" "Jun 2001" "Sep 2001" "Jan 2002"
[21] "Jun 2002" "Dec 2002" "Apr 2003" "Jun 2003"
[25] "Jan 2004" "Mar 2004" "Apr 2004" "May 2004"
[29] "Jun 2004" "Jun 2004" "Jul 2004" "Jul 2004"
[33] "Jul 2004" "Aug 2004" "Aug 2004" "Aug 2004"
[37] "Aug 2004" "Aug 2004" "Aug 2004" "Sep 2004"
[41] "Sep 2004" "Sep 2004" "Sep 2004" "Sep 2004"
[45] "Sep 2004" "Sep 2004" "Sep 2004" "May 2005"
[49] "May 2005" "May 2005" "May 2005" "May 2005"
[53] "May 2005" "Jun 2005" "Jun 2005" "Jun 2005"
[57] "Jul 2005" "Jul 2005" "Jul 2005" "Aug 2005"
[61] "Aug 2005" "Aug 2005" "Sep 2005" "Sep 2005"
[65] "Sep 2005" "May 2006" "May 2006" "May 2006"
[69] "Jun 2006" "Jun 2006" "Jun 2006" "Jul 2006"
[73] "Jul 2006" "Sep 2006" "Sep 2006" "Apr 2007"
[77] "May 2007" "Jul 2007" "Sep 2007" "Jan 2008"
[81] "Mar 2008" "May 2008"

我们需要一个更好的例子。所有这些日期都是在1998年

tapply(inp[ , 'logMosqm2'], format(as.Date(inp[ , 'Date'], origin=as.Date("1970-01-01") ),format="%Y" ),mean )
     1998 
-3.493211 
现在看来,您希望通过将格式更改为“$y-%m”来交付内容,以便2003-01与2004-01有所不同。(这不是你写的。)

但是,一旦我们有了一个合理的例子,我们可以应用一个限制函数,下面就是如何做到这一点:

tapply(inp[ , 'logMosqm2'], format(as.Date(inp[ , 'Date'], origin=as.Date("1970-01-01") ),format="%m" ),mean )
        03         04         06         10         12 
-4.0000000 -4.0000000 -4.0000000 -0.9592633 -4.0000000 
rd.txt
是我在添加
scan
的文本参数之前创建的一个函数,这样我就可以从海报上快速输入,比如你自己,你还没有学会用dput发布示例的优点:

rd.txt <-     function (txt, header = TRUE, ...) 
{        rd <- read.table(textConnection(txt), header = header, ...)
    closeAllConnections()
    rd}

inp <- data.matrix(rd.txt(" logMosqm2  Date
  -4.0000000 10296
  -4.0000000 10313
  -4.0000000 10342
  -4.0000000 10388
  -0.9592633 10526
  -4.0000000 10572") )

rd.txt需要一个更好的例子。所有这些日期都是在1998年

tapply(inp[ , 'logMosqm2'], format(as.Date(inp[ , 'Date'], origin=as.Date("1970-01-01") ),format="%Y" ),mean )
     1998 
-3.493211 
现在看来,您希望通过将格式更改为“$y-%m”来交付内容,以便2003-01与2004-01有所不同。(这不是你写的。)

但是,一旦我们有了一个合理的例子,我们可以应用一个限制函数,下面就是如何做到这一点:

tapply(inp[ , 'logMosqm2'], format(as.Date(inp[ , 'Date'], origin=as.Date("1970-01-01") ),format="%m" ),mean )
        03         04         06         10         12 
-4.0000000 -4.0000000 -4.0000000 -0.9592633 -4.0000000 
rd.txt
是我在添加
scan
的文本参数之前创建的一个函数,这样我就可以从海报上快速输入,比如你自己,你还没有学会用dput发布示例的优点:

rd.txt <-     function (txt, header = TRUE, ...) 
{        rd <- read.table(textConnection(txt), header = header, ...)
    closeAllConnections()
    rd}

inp <- data.matrix(rd.txt(" logMosqm2  Date
  -4.0000000 10296
  -4.0000000 10313
  -4.0000000 10342
  -4.0000000 10388
  -0.9592633 10526
  -4.0000000 10572") )

rd.txt使用
data.table
和示例

#Example
library(data.table)
set.seed(123)
all_dates <- as.numeric(seq(as.Date("2004/01/01"), as.Date("2008/12/31"), "day"))
dates <- sample(all_dates, 1000, replace = T)
dt <- data.table(x = rnorm(1000), dates = dates)
dt
#               x dates
#   1: -0.6018928 12943
#   2: -0.9936986 13858
#   3:  1.0267851 13165
#   4:  0.7510613 14031
#   5: -1.5091665 14136

dt[, dates := as.Date(dates, origin = "1970-01-01")]
dt
#               x      dates
#   1: -0.6018928 2005-06-09
#   2: -0.9936986 2007-12-11
#   3:  1.0267851 2006-01-17
#   4:  0.7510613 2008-06-01
#   5: -1.5091665 2008-09-14

#Relevant Code  
dt[, c("month", "year") := list(month(dates), year(dates))]
dt[year %in% c(2004, 2005, 2006), mean(x), by = month]
#    month           V1
# 1:     6 -0.044292743
# 2:     1  0.078148206
# 3:     3  0.062165254
# 4:     8 -0.149267201
# 5:    10 -0.024994773
# 6:     4  0.159856357
# 7:    11 -0.028046083
# 8:     7  0.019404375
# 9:     9  0.117634410
#10:    12  0.074059451
#11:     2 -0.001347801
#12:     5  0.096914779
#示例
库(数据表)
种子集(123)

所有日期使用
数据。表格
举例说明

#Example
library(data.table)
set.seed(123)
all_dates <- as.numeric(seq(as.Date("2004/01/01"), as.Date("2008/12/31"), "day"))
dates <- sample(all_dates, 1000, replace = T)
dt <- data.table(x = rnorm(1000), dates = dates)
dt
#               x dates
#   1: -0.6018928 12943
#   2: -0.9936986 13858
#   3:  1.0267851 13165
#   4:  0.7510613 14031
#   5: -1.5091665 14136

dt[, dates := as.Date(dates, origin = "1970-01-01")]
dt
#               x      dates
#   1: -0.6018928 2005-06-09
#   2: -0.9936986 2007-12-11
#   3:  1.0267851 2006-01-17
#   4:  0.7510613 2008-06-01
#   5: -1.5091665 2008-09-14

#Relevant Code  
dt[, c("month", "year") := list(month(dates), year(dates))]
dt[year %in% c(2004, 2005, 2006), mean(x), by = month]
#    month           V1
# 1:     6 -0.044292743
# 2:     1  0.078148206
# 3:     3  0.062165254
# 4:     8 -0.149267201
# 5:    10 -0.024994773
# 6:     4  0.159856357
# 7:    11 -0.028046083
# 8:     7  0.019404375
# 9:     9  0.117634410
#10:    12  0.074059451
#11:     2 -0.001347801
#12:     5  0.096914779
#示例
库(数据表)
种子集(123)

所有日期使用年和月的列生成数据框,然后根据您想要的年进行子集,并使用
tapply
获得平均因子:

d = data.frame(x=1:9,y=c(2001,2001,2001,2002,2002,2002,2003,2003,2003),
     month=rep(c('a','b','c'),3) )
ds = subset(d,y>2001)
tapply(ds$x,ds$month,mean)
数据如下所示:

  x    y z
1 1 2001 a
2 2 2001 b
3 3 2001 c
4 4 2002 a
5 5 2002 b
6 6 2002 c
7 7 2003 a
8 8 2003 b
9 9 2003 c
输出为:

  a   b   c 
5.5 6.5 7.5 

用年和月的列生成数据框,然后用所需年份的子集,并使用
tapply
获得平均因子:

d = data.frame(x=1:9,y=c(2001,2001,2001,2002,2002,2002,2003,2003,2003),
     month=rep(c('a','b','c'),3) )
ds = subset(d,y>2001)
tapply(ds$x,ds$month,mean)
数据如下所示:

  x    y z
1 1 2001 a
2 2 2001 b
3 3 2001 c
4 4 2002 a
5 5 2002 b
6 6 2002 c
7 7 2003 a
8 8 2003 b
9 9 2003 c
输出为:

  a   b   c 
5.5 6.5 7.5 

谢谢大家。在你的帮助下,我已经解决了这个问题。这就是我所做的

library(zoo)
camm.recent$yearmon <- as.yearmon(camm.recent$Date)

camm.recent$avglogMosqm2[camm.recent$yearmon > "Dec 2003" &
                           camm.recent$yearmon < "Jan 2007"] <-
     subset(tapply(camm.recent$logMosqm2, camm.recent$yearmon,
                   mean), camm.recent$yearmon > "Dec 2003" &
                     camm.recent$yearmon < "Jan 2007")
camm.recent$avglogMosqm2[camm.recent$yearmon < "Jan 2004"] <-
   camm.recent$logMosqm2[camm.recent$yearmon < "Jan 2004"]

camm.recent$avglogMosqm2[camm.recent$yearmon > "Dec 2006"] <-
  camm.recent$logMosqm2[camm.recent$yearmon > "Dec 2006"]

camm.recent$newdate[camm.recent$yearmon > "Dec 2003" &
                           camm.recent$yearmon < "Jan 2007"] <-
  subset(tapply(camm.recent$Date, camm.recent$yearmon,
                mean), camm.recent$yearmon > "Dec 2003" &
           camm.recent$yearmon < "Jan 2007")
camm.recent$newdate[camm.recent$yearmon < "Jan 2004"] <-
  camm.recent$Date[camm.recent$yearmon < "Jan 2004"]

camm.recent$newdate[camm.recent$yearmon > "Dec 2006"] <-
  camm.recent$Date[camm.recent$yearmon > "Dec 2006"]
图书馆(动物园)
camm.最近$yearmon“2003年12月”&
camm.最近的$yearmon<“2007年1月”]“2003年12月”&
camm.最近$yearmon<“2007年1月”)
camm.近期$avglogMosqm2[camm.近期$yearmon<“2004年1月”]“2006年12月”]“2006年12月”]
camm.recent$newdate[camm.recent$yearmon>“2003年12月”&
camm.最近的$yearmon<“2007年1月”]“2003年12月”&
camm.最近$yearmon<“2007年1月”)
camm.recent$newdate[camm.recent$yearmon<“2004年1月”]“2006年12月”]“2006年12月”]

感谢大家。在你的帮助下,我已经解决了这个问题。这就是我所做的

library(zoo)
camm.recent$yearmon <- as.yearmon(camm.recent$Date)

camm.recent$avglogMosqm2[camm.recent$yearmon > "Dec 2003" &
                           camm.recent$yearmon < "Jan 2007"] <-
     subset(tapply(camm.recent$logMosqm2, camm.recent$yearmon,
                   mean), camm.recent$yearmon > "Dec 2003" &
                     camm.recent$yearmon < "Jan 2007")
camm.recent$avglogMosqm2[camm.recent$yearmon < "Jan 2004"] <-
   camm.recent$logMosqm2[camm.recent$yearmon < "Jan 2004"]

camm.recent$avglogMosqm2[camm.recent$yearmon > "Dec 2006"] <-
  camm.recent$logMosqm2[camm.recent$yearmon > "Dec 2006"]

camm.recent$newdate[camm.recent$yearmon > "Dec 2003" &
                           camm.recent$yearmon < "Jan 2007"] <-
  subset(tapply(camm.recent$Date, camm.recent$yearmon,
                mean), camm.recent$yearmon > "Dec 2003" &
           camm.recent$yearmon < "Jan 2007")
camm.recent$newdate[camm.recent$yearmon < "Jan 2004"] <-
  camm.recent$Date[camm.recent$yearmon < "Jan 2004"]

camm.recent$newdate[camm.recent$yearmon > "Dec 2006"] <-
  camm.recent$Date[camm.recent$yearmon > "Dec 2006"]
图书馆(动物园)
camm.最近$yearmon“2003年12月”&
camm.最近的$yearmon<“2007年1月”]“2003年12月”&
camm.最近$yearmon<“2007年1月”)
camm.近期$avglogMosqm2[camm.近期$yearmon<“2004年1月”]“2006年12月”]“2006年12月”]
camm.recent$newdate[camm.recent$yearmon>“2003年12月”&
camm.最近的$yearmon<“2007年1月”]“2003年12月”&
camm.最近$yearmon<“2007年1月”)
camm.recent$newdate[camm.recent$yearmon<“2004年1月”]“2006年12月”]“2006年12月”]

使用测试数据集
camm.recent
在最后定义,我们创建了一个分组变量
g
,它最初由序列号组成。对于2004年和2006年(含)之间的任何重复的
yearmon
,我们将序列号替换为该
yearmon
之前第一次出现的序列号,方法是用
NA
填充重复项,并使用
NA.locf
。然后使用
aggregate
计算平均值

# this uses the test data set defined at the end
library(zoo)

# create an appropriate grouping variable, g
ym <- camm.recent$yearmon
between <- ym >= "Jan 2004" & ym <= "Dec 2006"
g <- replace(seq_along(ym), duplicated(ym) & between, NA)
g <- na.locf(g)

# aggregate by g
aggregate(camm.recent, data.frame(g), mean)[-1] # -1 removes g from output
#使用末尾定义的测试数据集
图书馆(动物园)
#创建适当的分组变量g

ym使用测试数据集
camm.recent
在最后定义,我们创建了一个分组变量
g
,它最初由序列号组成。对于2004年和2006年(含)之间的任何重复的
yearmon
,我们将序列号替换为该
yearmon
之前第一次出现的序列号,方法是用
NA
填充重复项,并使用
NA.locf
。然后使用
aggregate
计算平均值

# this uses the test data set defined at the end
library(zoo)

# create an appropriate grouping variable, g
ym <- camm.recent$yearmon
between <- ym >= "Jan 2004" & ym <= "Dec 2006"
g <- replace(seq_along(ym), duplicated(ym) & between, NA)
g <- na.locf(g)

# aggregate by g
aggregate(camm.recent, data.frame(g), mean)[-1] # -1 removes g from output
#使用末尾定义的测试数据集
图书馆(动物园)
#创建适当的分组变量g

ym日期格式是什么?数据最初来自于一个.csv文件,其中日期格式为1993年10月14日。我用read.csv将它们读入
R
。这些都是(我想)未格式化的日期。我仍然很困惑。所以上面的
10296
10/2/96
?你必须回到年和月来做平均值,所以可能只需将
Date
分割成
year
month
字段,并用这些字段做平均值。如果你想按年/月来做总结,那么你应该这么说。说“按月份”意味着所有一月的值一起被格式化。日期格式是什么?数据最初来自一个.csv文件,其中日期格式为1993年10月14日。我用read.csv将它们读入
R
。这些都是(我想)未格式化的日期。我仍然很困惑。所以上面的
10296
10/2/96
?你必须回到年和月来做平均值,所以可能只需将
Date
分割成
year
month
字段,并用这些字段做平均值。如果你想按年/月来做总结,那么你应该这么说。说“按月份”意味着所有一月的值一起得到。什么是inp?怎么