Last active
January 18, 2020 18:27
-
-
Save joeschofield0/99669d0850539e3dd2333c844813bb63 to your computer and use it in GitHub Desktop.
Multicolumn melt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(reshape2) | |
set.seed(3) | |
patient <- c(1:10) | |
gender <- sample(c("M", "F"), size = 10, replace = TRUE) | |
age <- sample(20:60, size = 10, replace = TRUE) | |
drug1 <- sample(c("A", "B", "C"), 10, replace = TRUE) | |
dose1 <- sample(10:200, size = 10, replace = TRUE) | |
dosecat1 <- sample(c("low", "med", "high"), size = 10, replace = TRUE) | |
date1 <- Sys.Date() + sample(-1:-90, 10) | |
drug2 <- sample(c("B", "D", "E"), 10, replace = TRUE) | |
dose2 <- sample(10:200, size = 10, replace = TRUE) | |
dosecat2 <- sample(c("low", "med", "high"), size = 10, replace = TRUE) | |
date2 <- Sys.Date() + sample(-1:-90, 10) | |
df <- data.frame(patient, gender, age, drug1, dose1, dosecat1, date1, drug2, dose2, dosecat2, date2) | |
# -- Melt attempt 1 -- | |
# Warning: Attributes are not identical across variables (drug = chr, dose = int) | |
# df_long <- melt(df, id.vars = "patient", measure.vars = c("drug1", "dose1", "drug2", "dose2")) | |
# -- Melt attempt 2 -- | |
# Getting closer, but losing gender, age, dose, date columns | |
df_long <- melt(df, id.vars = "patient", measure.vars = c("drug1", "drug2")) | |
df_long[order(df_long$patient), ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the df format I am aiming for...
Output from:
dput(read.table("clipboard", sep = "\t", header = TRUE))