嗨,我是r的新手,我必须在下面解决这个问题
考虑我有三年(36个月)以下的数据
psl(Pascal) tos(K) ua(m/s) va(m/s)
100828.2422 300.6350403 -2.908552885 -5.041597843
100865.375 300.6374512 -1.974732399 -3.884183884
100781.0156 301.3094177 -1.220992327 -2.782843351
100842.8359 302.0706787 -0.3339158 -2.049038887
100618.3516 303.1063843 0.983095765 0.187034369
100679.1641 302.6870117 2.236735344 3.573194027
100818.4219 302.1689758 1.754112124 2.603042126
100788.2266 302.3732605 -0.338575393 2.111710548
100712.1406 302.4180908 -1.487656116 1.750846267
100731.6406 302.4753723 -0.984614015 1.460931182
100643.4844 302.334198 0.101298548 0.029232573
100622.4297 302.1279297 -2.22670269 -0.482721329
100709.9453 301.0938721 -3.413815022 -2.25238204
100748.8516 300.9854736 -3.758933067 -1.831826091
100696.9063 301.7830505 -3.490354538 -1.195819378
100746.9063 302.3372192 -1.486986399 0.289745539
100676.6875 302.3781433 1.985690475 2.395581245
100720.4063 302.1488037 2.89744854 2.710779667
100734.3516 301.815155 3.502389193 3.37501812
100760.0078 301.9842834 1.93155086 3.437204123
100837.7109 302.2229614 3.258229494 2.887587547
100803.9375 302.8745422 2.309668064 0.393238485
100627.1563 302.821106 -0.111538976 -1.064256191
100740.8047 302.0934143 -1.853200674 -3.353952885
100871.6953 301.3536682 -3.842114687 -4.386196136
100797.3203 301.2049255 -4.432024479 -4.506450653
100762.2344 301.9281921 -3.15829277 -2.483273268
100761.7734 302.6669312 -0.989763916 -0.868626654
100717.8828 303.0666809 2.604245424 1.605101585
100726.2656 302.9170837 2.931357622 2.646541834
100754.5156 302.7220459 2.797704458 3.948915482
100771.8438 302.7012939 1.819163561 3.766605854
100741.5859 302.6376038 3.746546745 3.388780594
100825.75 302.8607178 2.811676979 0.938653767
100792.7031 302.8861694 0.876988828 -1.316085815
100783.8438 302.0106506 -0.735642731 -3.267586231
我想要的是找到数据的最大价值" tos(K)"每12个月一次。 显示的结果应为
tos(K)
[1]303.1063843
[2]302.8745422
[3]303.0666809
考虑第一个数据是1月1日,最后数据是Dis 3年。
如何在之后绘制块最大值?
任何帮助将不胜感激。谢谢你的时间。
这是我尝试过的..首先手动添加年份列并删除第1列,第3列和第4列,因为我只需要tos(K)数据集。我将tos(K)改为温度。
year temperature
2005 300.6350403
2005 300.6374512
2005 301.3094177
2005 302.0706787
2005 303.1063843
2005 302.6870117
2005 302.1689758
2005 302.3732605
2005 302.4180908
2005 302.4753723
2005 302.334198
2005 302.1279297
2006 301.0938721
2006 300.9854736
2006 301.7830505
2006 302.3372192
2006 302.3781433
2006 302.1488037
2006 301.815155
2006 301.9842834
2006 302.2229614
2006 302.8745422
2006 302.821106
2006 302.0934143
2007 301.3536682
2007 301.2049255
2007 301.9281921
2007 302.6669312
2007 303.0666809
2007 302.9170837
2007 302.7220459
2007 302.7012939
2007 302.6376038
2007 302.8607178
2007 302.8861694
2007 302.0106506
我的编码如下:
malaysia<-read.table("datamalaysiatry.csv",header=T, sep=",")
aggregate(temperature ~ year , data = malaysia, max)
结果显示:
year temperature
1 2016 303.1064
2 2017 302.8745
3 2018 303.0667
但正如我在评论中所述,我有数以千计的数据集。我真的需要花时间手动添加年份列。
答案 0 :(得分:0)
您需要一个变量/列来定义年份(12个月)。 然后每年你都可以获得最大值tos(K)。
请注意,由于某些字符,列名称会自动更改。
检查下面的示例:
library(dplyr)
#import data
df <- read.table(text = " psl(Pascal) tos(K) ua(m/s) va(m/s)
100828.2422 300.6350403 -2.908552885 -5.041597843
100865.375 300.6374512 -1.974732399 -3.884183884
100781.0156 301.3094177 -1.220992327 -2.782843351
100842.8359 302.0706787 -0.3339158 -2.049038887
100618.3516 303.1063843 0.983095765 0.187034369
100679.1641 302.6870117 2.236735344 3.573194027
100818.4219 302.1689758 1.754112124 2.603042126
100788.2266 302.3732605 -0.338575393 2.111710548
100712.1406 302.4180908 -1.487656116 1.750846267
100731.6406 302.4753723 -0.984614015 1.460931182
100643.4844 302.334198 0.101298548 0.029232573
100622.4297 302.1279297 -2.22670269 -0.482721329
100709.9453 301.0938721 -3.413815022 -2.25238204
100748.8516 300.9854736 -3.758933067 -1.831826091
100696.9063 301.7830505 -3.490354538 -1.195819378
100746.9063 302.3372192 -1.486986399 0.289745539
100676.6875 302.3781433 1.985690475 2.395581245
100720.4063 302.1488037 2.89744854 2.710779667
100734.3516 301.815155 3.502389193 3.37501812
100760.0078 301.9842834 1.93155086 3.437204123
100837.7109 302.2229614 3.258229494 2.887587547
100803.9375 302.8745422 2.309668064 0.393238485
100627.1563 302.821106 -0.111538976 -1.064256191
100740.8047 302.0934143 -1.853200674 -3.353952885
100871.6953 301.3536682 -3.842114687 -4.386196136
100797.3203 301.2049255 -4.432024479 -4.506450653
100762.2344 301.9281921 -3.15829277 -2.483273268
100761.7734 302.6669312 -0.989763916 -0.868626654
100717.8828 303.0666809 2.604245424 1.605101585
100726.2656 302.9170837 2.931357622 2.646541834
100754.5156 302.7220459 2.797704458 3.948915482
100771.8438 302.7012939 1.819163561 3.766605854
100741.5859 302.6376038 3.746546745 3.388780594
100825.75 302.8607178 2.811676979 0.938653767
100792.7031 302.8861694 0.876988828 -1.316085815
100783.8438 302.0106506 -0.735642731 -3.267586231 ",header = TRUE)
df %>%
mutate(year = c(rep("year1",12), rep("year2",12), rep("year3",12))) %>% # create -somehow- a variable that shows the time-period (12 months)
group_by(year) %>% # group by that variable
summarise(MaxTos = max(tos.K.)) # find maximum value for each group
# year MaxTos
# (chr) (dbl)
# 1 year1 303.1064
# 2 year2 302.8745
# 3 year3 303.0667
答案 1 :(得分:0)
另一种选择是转换为时间序列对象。按照AntoniosK的代码创建数据框,然后应用以下代码
x<-ts(df$tos.K., freq=12)
aggregate(x, FUN=max)