I need some help with my data. I am going to explain it better using an example. Here is the dput()
data:
data <- structure(list(SEQ = c(1, 2, 2, 2, 2, 2),
PR = structure(c(1L, 2L, 3L, 4L, 5L, 8L), .Label = c("AHE",
"AHE", "BHE", "BTH", "CHE", "CTH", "DHE",
"DS", "DTH"), class = "factor"), mittel = c(1.33,
2, 0.17, 0.33, 0, 0), max = c(1.33, 2, 0.17, 0.33, 0, 0),
s = c(NaN, NaN, NaN, NaN, NaN, NaN), n = c(1L, 1L, 1L, 1L,
1L, 1L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-6L), .Names = c("SEQ", "PR", "mittel", "max", "s",
"n"))
It looks like this exactly:
SEQ PR mittel max s n
1 1 AHE 1.33 1.33 NaN 1
2 2 AHE 2.00 2.00 NaN 1
3 2 BHE 0.17 0.17 NaN 1
4 2 BTH 0.33 0.33 NaN 1
5 2 CHE 0.00 0.00 NaN 1
6 2 DS 0.00 0.00 NaN 1
I would like to paste the unique values of column PR with the column names: mittel, max, s and n. Then transpose the table and use the SEQ column as header for the transposed table. It would look smthg like this:
1 2
AHE_mittel 1.33 2.0
AHE_max 1.33 2.0
AHE_s NaN NaN
AHE_n 1 1
...
[ANSWER]
I would like to thank everyone for help! I have noticed (thank to other users) that it is sort of a duplicate question. At the end i have used this code using dcast
:
data <- melt(data, id=c(1:2))
data$id <- paste(data$PR, data$variable, sep="_")
data <- dcast(data, ...~SEQ, median)
Cheers