I have a dataframe (df1
) and would like to rearrange the information for further processing. However, I am not sure how to convert the unique Site
values into headers in the table. How could I accomplish this?
Here is the example data
df1 <- data.frame(matrix(ncol = 4, nrow = 27))
x <- c("Date","Distance","Site","MeanVal")
colnames(df1) <- x
df1$Date <- c("2020-06-01","2020-06-01","2020-06-01","2020-06-01","2020-06-01","2020-06-01","2020-06-01","2020-06-01","2020-06-01",
"2020-07-01","2020-07-01","2020-07-01","2020-07-01","2020-07-01","2020-07-01","2020-07-01","2020-07-01","2020-07-01",
"2020-08-01","2020-08-01","2020-08-01","2020-08-01","2020-08-01","2020-08-01","2020-08-01","2020-08-01","2020-08-01")
df1$Date <- as.Date(df1$Date, format = "%Y-%m-%d")
df1$Distance <- as.numeric(rep(c("0.5","1.5","2.5"),9))
df1$Site <- c("1A","1A","1A","2B","2B","2B","3C","3C","3C",
"1A","1A","1A","2B","2B","2B","3C","3C","3C",
"1A","1A","1A","2B","2B","2B","3C","3C","3C")
set.seed(123)
df1$MeanVal <- round(rnorm(27,100,50), digits = 0)
The output should look something like this
Date Distance 1A 2B 3C
2020-06-01 0.5 108 121 144
2020-06-01 1.5 43 85 141
2020-06-01 2.5 163 145 134
2020-07-01 0.5 128 81 37
2020-07-01 1.5 97 65 208
2020-07-01 2.5 85 90 160
2020-08-01 0.5 44 139 99
2020-08-01 1.5 80 96 98
2020-08-01 2.5 77 113 168