日、月和年平均值

  • 本文关键字:年平均 平均值 r
  • 更新时间 :
  • 英文 :


我有每小时的数据。我需要转换成每日,每月,然后在年度。

此外,其中缺少一些日期,所以我也想包括在内。

#Date
24/02/2000/05:25:00 NaN NaN NaN

2000年2月26日/05:10:00 0.227 0.2002496 0.20093782000年2月26日06时50分2000年2月27日05:55:00 0.21 0.1687891 0.16305722000年2月28日05:00:00 NaN NaN 0.12656962000年2月28日06:35:00 0.136 0.1446176 0.14790672000年2月29日05:40:00 0.293 0.2279881 0.19005142000年3月1日04:45:00 NaN NaN01/03/2000/06:25:00 0.322 0.3068518 0.288057902/03/2000/05:30:00.332 0.2793714 0.23916222000年3月2日07:05:00 NaN NaN03/03/2000/06:10:00.335 0.21151302 0.22181392000年3月4日05:15:00 0.1 0.1138773 0.11689122000年3月4日06时55分NaN NaN2005/03/2000/06:00:00 0.117 0.1333082 0.1471452000年3月6日05:05:00硝酸钠0.2426362 0.24018712000年3月6日06:40:00 NaN 0.32587 0.28450672000年3月7日05:45:00 0.323 0.3143821 0.30966622000年3月8日04:50:00 NaN NaN2000年3月8日06时30分0.236 0.23232 0.23001072000年3月10日06:20:00 0.113 0.1429935 0.14537742000年3月11日05:25:00 0.276 0.3238274 0.31505852000年3月11日07:00:0012/03/2000/06:05:00 0.215 0.2537585 0.25123742000年3月13日/05:10:00 0.163 0.2273455 0.26793522000年3月13日06时50分2000年3月14日05:55:00 0.09 0.1311507 0.17610562000年3月15日05:00:00 NaN NaN 0.14473482000年3月15日06:35:00 0.125 0.1232291 0.13877822000年3月16日05:40:00 0.019 0.06970426 0.116022000年3月17日04:45:00 NaN NaN2000年3月17日06:25:00 0.194 0.1964414 0.18744032000年3月18日/05:30 0.263 0.2749394 0.2421992000年3月18日07:05:002000年3月19日/06:10:00 0.217 0.217737 0.211837062000年3月20日05:15:00 0.253 0.2307511 0.20898912000年3月20日06时55分NaN NaN2000年3月21日06:00:00 0.282 0.2413632 0.25112352000年3月22日05:05:00北纬0.382685 0.39446362000年3月22日06时45分NaN 0.2734097 0.2414422000年3月23日05:50:00 0.347 0.3289219 0.30038482000年3月24日04:50:00 NaN NaN2000年3月24日06时30分0.18 0.1892378 0.20215162000年3月25日05:35:00 0.216 0.1871835 0.2067622000年3月26日/06:20:00 0.189 0.1836237 0.21164532000年3月27日05:25:00 0.195 0.1817446 0.18044642000年3月27日07:00:002000年3月28日06:05:00 0.208 0.168515 0.18191152000年3月29日05:10:00 0.162 0.1598227 0.16895232000年3月29日06时50分2000年3月30日05:55:00 0.145 0.1472181 0.17237742000年3月31日05:00:00 NaN NaN 0.1577232000年3月31日06:35:00 0.226 0.2108984 0.2339231

我猜您正在讨论将日期变量拆分为年、月和日,然后您想计算另一个变量的一些分组统计信息,而您的示例中没有包括这些变量。如果是这种情况,您可以执行以下操作:

# load package 
library(dplyr)
#Date
Date <- data.frame( Date =strptime(c("24/02/2000/05:25:00", 
"26/02/2000/05:10:00",
"26/02/2000/06:50:00",
"27/02/2000/05:56:00",
"28/02/2000/05:00:00",
"28/02/2000/06:35:00",
"29/02/2000/05:40:00",
"01/03/2000/04:45:00",
"01/03/2000/06:25:00",
"02/03/2000/05:30:00",
"02/03/2000/07:05:00",
"03/03/2000/06:10:00",
"04/03/2000/05:15:00",
"04/03/2000/06:55:00",
"05/03/2000/06:00:00",
"06/03/2000/05:05:00",
"06/03/2000/06:40:00",
"07/03/2000/05:45:00",
"08/03/2000/04:50:00",
"08/03/2000/06:30:00",
"10/03/2000/06:20:00",
"11/03/2000/05:25:00",
"11/03/2000/07:00:00",
"12/03/2000/06:05:00",
"13/03/2000/05:10:00",
"13/03/2000/06:50:00",
"14/03/2000/05:55:00",
"15/03/2000/05:00:00",
"15/03/2000/06:35:00",
"16/03/2000/05:40:00",
"17/03/2000/04:45:00",
"17/03/2000/06:25:00",
"18/03/2000/05:30:00",
"18/03/2000/07:05:00",
"19/03/2000/06:10:00",
"20/03/2000/05:15:00",
"20/03/2000/06:55:00",
"21/03/2000/06:00:00",
"22/03/2000/05:05:00",
"22/03/2000/06:45:00",
"23/03/2000/05:50:00",
"24/03/2000/04:50:00",
"24/03/2000/06:30:00",
"25/03/2000/05:35:00",
"26/03/2000/06:20:00",
"27/03/2000/05:25:00",
"27/03/2000/07:00:00",
"28/03/2000/06:05:00",
"29/03/2000/05:10:00",
"29/03/2000/06:50:00",
"30/03/2000/05:55:00",
"31/03/2000/05:00:00",
"31/03/2000/06:35:00"), format = "%d/%m/%Y/%H:%M:%S"))

# Split your Date variable in days, months, and years
Date[,"Year"] <- format(Date$Date, format = "%Y")
Date[,"Month"] <- format(Date$Date, format = "%m")
Date[,"Day"] <- format(Date$Date, format = "%d")
# Make up some random variable to calculate summary statistics on
Date[,"Random"] <- sample(seq(1,7,1),size=dim(Date)[1], replace = TRUE)
# Now you can calculate grouped statistics by day, month, or year
MonthMean <- Date %>%
group_by(Month) %>%
select(Month, Random) %>%
summarise(Mean = mean(Random))
# Output 
# A tibble: 2 × 2
Month     Mean
<chr>    <dbl>
1    02 3.142857
2    03 4.217391

我已经拆分了天、月和年的数据,然后计算了Daymean、Monthlymean和Annualmean

使用代码:

# open the file
file1 <-read.table(file.choose(), header=T)
# View the content of the file
View(file1)
# assign the date
as.character(file1$Date) -> file1$date
time <- as.Date( file1$date, "%d/%m/%Y")
# seperate the day, month, year
file1[,"Year"] <- format(time,  format = "%Y")
file1[,"Month"] <- format(time, format = "%m")
file1[,"Day"] <- format(time, format = "%d")
# to see the updates file
View(file1)
# avearaging the dayily mean then same as month wise 
aggregate(file1[, 2:4], list(file$Day), mean, na.rm=T)

最新更新