Very similar question to this one, however there are some fundamental differences.
I have a data set of a Timestamp, 4 measurement columns, and 4 state columns:
structure(list(Timestamp = structure(c(1409544002, 1409544006,
1409544010, 1409544014, 1409544018, 1409544022), class = c("POSIXct",
"POSIXt"), tzone = ""), A = c(0, 0, 0, 0, 0, 0), B = c(20.77579,
21.05727, 21.81632, 21.36299, 21.18629, 21.34721), C = c(16.25537,
16.45496, 16.70933, 16.1526, 16.60963, 16.76558), D = c(0, 0,
0, 0, 0, 0), SA = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1",
"0"), class = "factor"), SB = structure(c(1L, 1L, 1L, 1L, 1L,
1L), .Label = c("1", "0"), class = "factor"), SC = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("1", "0"), class = "factor"),
SD = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1",
"0"), class = "factor")), .Names = c("Timestamp", "A", "B",
"C", "D", "SA", "SB", "SC", "SD"), row.names = c(NA, 6L), class = "data.frame")
I want to calculate the median of the columns that are on, as indicated by a 1 in the S* columns.
So far I can find which measurement columns to use row by row using this:
foo[i, c(which(x = foo[i, 6:9] == 1, arr.ind = FALSE) + 1)]
where i
is the row number.
And this is as far as I get without my code getting too complicated. I was thinking I could create a new data frame by binding the columns I got with the line of code above (after a row by row for
loop) to the Timestamp, fill in empty spots with NAs, calculate the median of that data frame, and finally bind the median to the original data frame. But there has to be a better way!
Any ideas?
Edit:
Output should look like this:
structure(list(Timestamp = structure(c(1409544002, 1409544006,
1409544010, 1409544014, 1409544018, 1409544022), class = c("POSIXct",
"POSIXt"), tzone = ""), A = c(0, 0, 0, 0, 0, 0), B = c(20.77579,
21.05727, 21.81632, 21.36299, 21.18629, 21.34721), C = c(16.25537,
16.45496, 16.70933, 16.1526, 16.60963, 16.76558), D = c(0, 0,
0, 0, 0, 0), SA = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1",
"0"), class = "factor"), SB = structure(c(1L, 1L, 1L, 1L, 1L,
1L), .Label = c("1", "0"), class = "factor"), SC = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("1", "0"), class = "factor"),
SD = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1",
"0"), class = "factor"), Median = c(18.51558, 18.756115,
19.262825, 18.757795, 18.89796, 19.056395)), .Names = c("Timestamp",
"A", "B", "C", "D", "SA", "SB", "SC", "SD", "Median"), row.names = c(NA,
6L), class = "data.frame")