I have the following data frame:
df <- data.frame(id = c("1","1","1","1","2","2","4","4"),
shift = c("1","1","1","2","1","2","1","1"),
measurement = c("A","B","C","D","A","B","A","C"),
critical_value = c("130","yes","55","1","115","no","110","54"))
id shift measurement critical_value
1 1 1 A 130
2 1 1 B yes
3 1 1 C 55
4 1 2 D 1
5 2 1 A 115
6 2 2 B no
7 4 1 A 110
8 4 1 C 54
How can transform this data so that every row in which it has the same id and shift to merge by different measurement with critical_value. So that it would look like this:
id shift A B C D
1 1 1 130 yes 55 NA
4 1 2 NA NA NA 1
5 2 1 3 NA NA NA
6 2 2 NA 7 NA NA
7 4 1 2 NA 5 NA
I tried using ifelse
with group_by
and summarise_all
from the dplyr
package. But this resulted in the following faulty output:
id shift A B C D
1 1 1 4 8 6 NA
4 1 2 NA NA NA 1
5 2 1 3 NA NA NA
6 2 2 NA 7 NA NA
7 4 1 2 NA 5 NA
My code is:
library(dplyr)
df <- data.frame(id = c("1","1","1","1","2","2","4","4"),
shift = c("1","1","1","2","1","2","1","1"),
measurement = c("A","B","C","D","A","B","A","C"),
critical_value = c("130","yes","55","1","115","no","110","54"))
df$A <- ifelse(df$measurement == "A", df$critical_value, NA)
df$B <- ifelse(df$measurement == "B", df$critical_value, NA)
df$C <- ifelse(df$measurement == "C", df$critical_value, NA)
df$D <- ifelse(df$measurement == "D", df$critical_value, NA)
my_fun <- function(x) x[!is.na(x)]
df %>%
group_by("id","shift") %>%
summarise_all(funs(my_fun))
df$critical_value <- NULL
df$measurement <- NULL
Notes: - Output doesn't have to be NA - Rownames don't have to be kept - Complete dataset is 7million lines so if you have any advice on how to iterate this fast, please!