清理天气数据并计算平均值

时间:2019-01-28 19:57:41

标签: r

我每天有最低温度,最高温度,最低露点和最高露点。此数据包含NaN,因此我想进行各种操作;

  1. 按年份计算列中NaN的百分比以及整个期间(1948-2018年)的总百分比
  2. 如果一年中的NaN百分比超过10%,则排除该特定年份的所有数据(如果少于该数量则用于计算)。
  3. 如果遵循规则2,则计算最高和最低温度的年平均温度。 4.计算最高和最低露点温度的年平均露点
  4. 计算是否符合规则2的每年最低温度,最高温度,最低露点和最高露点
  5. 从3、4和5的结果中得出。我想制作一个趋势图,年份与变量。
  6. 我也想对每个变量运行t个测试。

我的数据看起来像

 Station Date    Month  Day Year    MaxTemp MinTemp MaxDewPoint MinDewPoint
    ORD 1/1/1948    1   1   1948    35.6    26.6    34.16         -27.4
    ORD 1/2/1948    1   2   1948    -2      -16     -16.96       -27.04
    ORD 1/3/1948    1   3   1948    -4      -26     -12            -26
    ORD 1/4/1948    1   4   1948    -5      -26     -15             -26
    ORD 1/5/1948    1   5   1948    8       -25     3               NaN
    ORD 1/6/1948    1   6   1948    -11     -25     -24            -25
    ORD 1/7/1948    1   7   1948    1       -23     NaN            -23
    ORD 1/8/1948    1   8   1948    1       -22     -9              NaN
    ORD 1/9/1948    1   9   1948    NaN     -22     -5             -22
    ORD 1/10/1948   1   10  1948    10      NaN     -2              -22
    ORD 1/11/1948   1   11  1948    -11     -21    -23              -21
    ORD 1/12/1948   1   12  1948    3       -12     -7.96        -20.92
    ORD 1/13/1948   1   13  1948    6.98    -7.6    -7.6         -20.2
    ORD 1/14/1948   1   14  1948    3.92    -9.4    -11.2        NaN
    ORD 1/15/1948   1   15  1948    6        -7    -5.98         NaN
    ORD 1/16/1948   1   16  1948    3       -11     -7.96       -20.02

到目前为止,我设法编写了几行代码,但是我不确定这是正确的代码还是最有效的代码。

install.packages("dplyr")
install.packages("stringr")
library(dplyr)
library(stringr)

#setting up workspace in the folder#
setwd("E:/Climate Data Analysis/Asignment 1")
#opening a CSV file in r program#
data<- read.csv("chiacagost.csv", header=TRUE, sep=",")
#making data frame of the variables#
dframe<- data.frame(data)
# Missing percentage of the data by column
MisMxTMP<-dframe%>%summarise(NAMisMxTMP=sum(is.na(Max.Temp)/length(Max.Temp)))*100
misMnTMP<-dframe%>%summarise(NAmisMnTMPL=sum(is.na(Min.Temp)/length(Min.Temp)))*100
MisMxDTMP<-dframe%>%summarise(NAMisMxDTMP=sum(is.na(Max.Dew.Point)/length(Max.Dew.Point)))*100
MisMnDTMP<-dframe%>%summarise(NAMisMnDTMP=sum(is.na(Min.Dew.Point)/length(Min.Dew.Point)))*100

#Calcualte Mean temperature and dew point

MeanTMP<-dframe%>% mutate(MeanTMP=rowMeans(cbind(dframe$Max.Temp,dframe$Min.Temp),na.rm=TRUE))
#Minimum Dew point had more than 12 percent of missing data but i could not locate which years so the code below doesnot exclude NA
MeanDTMP<-dframe%>% mutate(MeanDTMP=rowMeans(cbind(dframe$Max.Dew.Point,dframe$Min.Dew.Point),na.rm=TRUE)) 

从这个结果中,我打算分别获取所有4个变量(泰尔森和OLS)的年度趋势,并进行t检验。将所有结果包括在趋势图中。

0 个答案:

没有答案
相关问题