Dealing with factors in a dataframe in R

89 views Asked by At

I am relatively new to R and I am trying to process a lot of weather data stored in a data frame. My goal is to loop through the data and compare the Element column of my data frame to several values. I plan to do something different depending on the value of Element sort of like a traditional switch statement. The element value might be one of 24 different values for example TMAX or TMIN. Currently the element column is a factor with 24 levels. What is the fastest way to loop through this data running a different set of code depending on the value of Element? How do I compare the values in Element to check if it is equal to TMAX for example? Efficiency is very important. Any suggestions are appreciated.

Example:

library("sqldf")
library("RPostgreSQL")

width = c(11, 4, 2, 4)
label = c("ID", "Year", "Month", "Element")
for (i in 1:31)
{
  width = c(width, 5, 1, 1, 1)
  label = c(label, paste("Value ",i), paste("MFLAG ",i), paste("QFLAG ",i), paste("SFLAG", i))
}
test = read.fwf("ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/daily/all/USW00093734.dly", header = FALSE, fill = TRUE, widths = width, col.names = label)

#You need to set up your database for this to work
for(i in 1:length(test$ID))
{
  if(as.character(test$Element) == "TMAX")
  {
    for(j in 1:31)
    {
      insert_statement = paste("INSERT into noaa_temperature (id, max_temp, date) values (", test$ID[[i]],",",test[paste("Value..",j)],toString(as.Date(ISOdate(test$Year,test$Month,j))),")")
      sqldf(insert_statement)
    }
  }
  if((as.character(test$Element) == "WT01") || (as.character(test$Element) == "WT03")
  {
    for(j in 1:31)
    {
      if(test[paste("Value..",j)] == 1)
      {
        insert_statement = paste("INSERT into noaa_weather_type (id, code, date) values (", test$ID[[i]],",",toString(test$Element),toString(as.Date(ISOdate(test$Year,test$Month,j))),")")
        sqldf(insert_statement)
      }
    }
  }
}

I would like to execute a different section of code on each row depending on the value of the Element column in test.

0

There are 0 answers