I know that this activity might be a bit rough for folks who are new to coding. So, I thought that I would use this page to provide some tips and tricks to help you get started. I have listed them according to each question that you are trying to answer. As more questions come in, I hope to add to this page.
# Format a column in dataframe "mydata" to be dates # You have to tell R the format of the dates in the file mydata$Date<-strptime(mydata$Date, format="%Y-%m-%d") #but if you edited the file in Excel, you might need: mydata$Date<-strptime(mydata$Date, format="%m/%d/%y")
# Get a year's worth of dates dates1976<-mydata$Date[(mydata$Date >= "1976-01-01" & mydata$Date < "1977-01-01" )] # Get a year's worth of Max Temperature Normals maxtemps1976<-mydata$MaxTemperatureNormal[(mydata$Date >= "1976-01-01" & mydata$Date < "1977-01-01" )] #etcThen, make your plots using these new variables.
cumsum(...)
to perform this task.JanmeanTempdiffs<-mydata$AvgTemperature[(strftime(mydata$Date,"%m"))=="01"]-mydata$AvgTemperatureNormal[(strftime(mydata$Date,"%m"))=="01"]
JanmeanTempdiffs<-mydata$AvgTemperature[(strftime(mydata$Date,"%m"))=="01"]-mydata$AvgTemperatureNormal[(strftime(mydata$Date,"%m"))=="01"] Jan_dates<-mydata$Date[which(strftime(mydata$Date,"%m")=="01")]Now, to average these by year, we need to use the
aggregate(...)
function. This function takes one column of data and performs a function on it using grouping (specified in the "by
" column). In the case below, I create a list of years by which to do the averaging.
# use the aggregate function to get a mean January # departure by year. meanTempdiffs<-aggregate(JanmeanTempdiffs, by=list(strftime(Jan_dates,"%Y")), mean, na.rm=TRUE) colnames(meanTempdiffs) <- c("Year", "MeanDiff")Finally, we can sort the result by coldest January.
coldest_Jans<-monthyTempdiff[order(monthyTempdiff$MeanDiff),]
skip=2
to read.csv(...)
. This will skip the first two header rows of the file (you can rename the columns you want to keep). I also suggest using the parameter, colClasses = "character"
, so that you can do the data conversions yourself (better this way).# read the data # keep the date, time, direction, and speed columns # create a single date column. Here's an example # I labeled my unformatted date/time accordingly wind_data$date<-as.POSIXct(strptime(paste(wind_data$date_uf,wind_data$time_uf), format = "%Y%m%d %H%M", tz="UTC")) # convert wd and ws columns to numeric and set 999 to NA # plot the wind rose