I need to arrange a table by performing some formatting, with a table like
DT <- read.table(text =
"Year ST_ID N Overall Metric1 Metric2
1999 205 386 96.3 0 0
1999 205 15 0 0 0
1999 205 0 0 0 0
1999 205 0 0 0 NA
2000 205 440 100 0 0
2000 205 0 0 0 0
2000 205 0 0 NA 0
2000 205 0 0 0 NA", header = TRUE)
I need to obtain the following "Output" table.
Year ST_ID 1 2 3 4 Overall Metric1 Metric2
1999 205 386 15 0 0 96.3 0 NA
2000 205 440 0 0 0 100 NA NA
.
.
In the columns on the right, I want to aggregate any instances of NA => to NA else sum(values)
How can I achieve this using R?