I have two Excel sheets of climate data: temperature (dtT) and relative humidity (dtR). Each frame is 39 rows (monitoring sites) by 64 columns (61 days numbered from 121 to 181, “lat”, “lon”, and “county”). Both sheets are in the exact same order. I want to use these two datasets to calculate the “heat index” for each monitoring site on each day, filling up another dataframe of the same dimensions.
PROBLEM: I am attempting to use a nested loop, but all the values come back as zeros. Could it be that because the “county” column contains words, R is reading all the numbers as characters, and so it can’t perform the calculations? (**The columns specified may appear confusing here but i’m looking for general strategy)
dtT <- read.csv("C:/Users/Desktop/Tavg3.csv")
dtR <- read.csv("C:/Users/Desktop/RHavg3.csv")
# Make a new data frame
hi = cbind(dtT, dtR)
# Add empty columns for heat index
hi[paste("hi",121:181,sep="")] = 0
# Loop to fill each hi cell using NOAA formula
for(i in 1:length(hi$lat)){
t <- hi[i, 3:63]
r <- hi[i, 64:124]
h <- hi[i, 125:185]
for(j in 1:length(t)){
h[i,j] = -42.379 + (2.04901523*t[j]) + (10.14333127*r[j]) - (0.22475541*t[j]*r[j]) -
((6.83783*10^-3)*(t[j]^2)) - ((5.481717*10^-2)*(r[j]^2) ) + ((1.22874*10^3) - (t[j]^2)*r[j]) + ((8.5282*10^-4)*t[j]*(r[j]^2)) - ((1.99*10^-6)*(t[j]^2)*(r[j]^2))
}
}
hi[1:10, 130:140]
hi = hi[,c(1:2, 125:185]
In your code you update
h[i,j], but afterwards you look athi… What doeshlook like after the for-loop?Note that modifying
hwill not modifyhieven though conceptuallyhis a subset ofhi…You could probably just replace
h[i,j] =withhi[i,j+124] =