I have a dataframe as follows:
dat <- read.table(text=
"ID | Year | Month | Variable | Value1 | Value2 | Value3
1 | 1950 | 1 | PRCP | 0 | 1 | 0
1 | 1950 | 1 | TMAX | 52 | 51 | 52
1 | 1950 | 1 | TMIN | 41 | 41 | 39
1 | 1950 | 2 | PRCP | 1 | 0 | 1
1 | 1950 | 2 | TMAX | 55 | 57 | 58",
header=TRUE, sep="|")
There are 50 Station IDs, the years span 1950-2005, months 1-12, there are 3 weather variables (PRCP, TMAX, and TMIN), and then columns Value1-Value31 for each day of the month with the weather variable measurement.
I would like to create a dataframe that looks like this:
ID | Date | PRCP
1 | 1950-01-01 | 0
1 | 1950-01-02 | 1
1 | 1950-01-03 | 0
So far I've been able create 3 separate datasets for each weather variable, but I have no idea how to create the new columns and expand the rows accordingly (there will need to be 31 new rows for each day of the month). I'm fairly new to R and would appreciate any help - Thanks!