Problem and Data
I have following data frame. For each time_frame the corresponding value is in the diagonal. Here's the code to create this data frame:
> dput(foo)
structure(list(time_frame = c(20948, 23748, 24026, 24350, 24462
), col1 = c(1, 1, 1, 1, 1), col2 = c(2, 2, 2, 2, 2), col3 = c(1,
1, 1, 1, 1), col4 = c(3, 3, 3, 3, 3), col5 = c(1, 1, 1, 1, 1)), .Names = c("time_frame",
"col1", "col2", "col3", "col4", "col5"), row.names = c(NA, -5L
), class = "data.frame")
Required Output and What I tried
My required output is following:
foo2 <- data.frame(time_frame = c(20948, 23748, 24026, 24350, 24462),
turn_signal_state = c(1,2,1,3,1))
I am not sure what's the best way to get this output. So, I tried tidyr package as follows:
library(tidyr)
foo %>%
gather(col, turn_signal_state, -time_frame)
But as you see, this is not exactly what I need. Please guide me what will be the best approach to convert foo to foo2. I prefer using tidyverse. Also, the original file is a text file that I read using read.delim. Please let me know if it is possible to read in the foo2 format in the first place. Thanks.
