I have a couple of files that I am trying to convert to a wide format. However, I got it to work but I then realized that the way the data is displayed when was a little confusing for what I needed to use it for. For example, all of the data under the variable I have labeled WTMP was labeled "WYLD.January" for every year but I originally did not include a year indicator in the label, so for every year the label is just "WYLD.January". I changed the name of the months in the "months" column to reflect the year also - so instead of just "January", I changed it to "January_08, January_09...January_17, January_18".
But when I changed the months to reflect the dates, the data wouldn't no longer showed up in wide format in R and I'm not sure why because this was the only change I made
This is my current code. I am very new to R so I'm just not experienced enough to know what I need to change to fix this:
require(here)
require(data.table)
require(readxl)
baseline_upper_total = read_xlsx(
here(
"CT_total_upper.xlsx"))
baseline_upper_total
head(baseline_upper_total)
names(baseline_upper_total)
baseline_upper_total = data.table(baseline_upper_total)
names(baseline_upper_total)
baseline_upper_wide = dcast(
setDT(baseline_upper_total),
name ~ months,
value.var = c(
"PRECIP", "ET", "SURQ", "WYLD",
"FLOW_OUT", "SED_OUT", "DISOX_OUT", "TOT_N", "TOT_P", "SYLD"), fun.aggregate = mean)
I included a reproducible example someone recommended I include in a previous question I had, my data in total has 133 rows and 12 columns:
# name months PRECIP ET SURQ WYLD FLOW_OUT
#1 1080101 January_08 13.0 2.08 6.55 17.4 1712
#2 1080101 February_08 38.2 2.85 3.78 9.07 689
#3 1080101 March_08 27.5 5.69 13.5 28.3 1781
#4 1080101 April_08 22.6 10.2 8.57 29.1 3102
#5 1080101 May_08 6.53 13.5 0.0245 7.68 914
#6 1080101 June_08 35.4 23.2 1.21 9.83 816
#7 1080101 July_08 41.5 27.4 2.81 13.2 1056
#8 1080101 August_08 32.2 25.3 1.39 11.3 1269
#9 1080101 Sept_08 19.9 17.2 0.782 7.56 768
#10 1080101 October_08 27.2 10.4 1.73 8.82 730
This is a snippit of the data after it was transformed into the wide format. As you can see, none of the values are there, which is definitely not supposed to be like that.
I also tried using this method and the result was the same:
baseline_upper_wide = reshape(baseline_upper_total, idvar = "name", timevar = "months", direction = "wide")