I'm new to data analysis with R. Recently, I got a pre-formatted dataset of an environmental observing model, a subset of which is shown below:
date site obs mod site obs mod 2000-09-01 00:00:00 campus NA 61.63 city centre 66 56.69 2000-09-01 01:00:00 campus 52 62.55 city centre NA 54.75 2000-09-01 02:00:00 campus 52 63.52 city centre 56 54.65
Basically, the data includes time series of hourly observed and simulated pollutant concentrations at different sites in “repeating columns”, i.e. obsmod site (in the example I showed only 2 of all 75 sites). I read this "wide" dataset as a data frame and wanted to change it in a "narrower" format as:
date site obs mod 2000-09-01 00:00:00 campus NA 61.63 2000-09-01 01:00:00 campus 52 62.55 2000-09-01 02:00:00 campus 52 63.52 2000-09-01 00:00:00 city centre 66 56.69 2000-09-01 01:00:00 city centre NA 54.75 2000-09-01 02:00:00 city centre 56 54.65
I figured I should use the "reshape2" package for this. First I tried to melt and then dcast the data set:
test.melt <- melt(test.data, id.vars = "date", measure.vars = c("site", "obs", "mod"))
However, he returned only half of the data, i.e. all entries of the site (s) ("city center") after the first ("campus") were cut off:
date variable value 2001-01-01 00:00:00 site campus 2001-01-01 01:00:00 site campus 2001-01-01 02:00:00 site campus 2001-01-01 00:00:00 obs NA 2001-01-01 01:00:00 obs 52 2001-01-01 02:00:00 obs 52 2001-01-01 00:00:00 mod 61.63 2001-01-01 01:00:00 mod 62.55 2001-01-01 02:00:00 mod 63.52
Then I tried to redo:
test.recast <- recast(test.data, date ~ site + obs + mod)
However, he returned with an error message:
Error in eval(expr, envir, enclos) : object 'site' not found
I tried to find the previous questions, but could not find similar scenarios (correct me if I am wrong). Can someone please help me with this?
Thank you very much in advance!