Skip to content

Instantly share code, notes, and snippets.

@joeschofield0
Last active January 18, 2020 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeschofield0/99669d0850539e3dd2333c844813bb63 to your computer and use it in GitHub Desktop.
Save joeschofield0/99669d0850539e3dd2333c844813bb63 to your computer and use it in GitHub Desktop.
Multicolumn melt
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), ]
@joeschofield0
Copy link
Author

joeschofield0 commented Jan 18, 2020

This is the df format I am aiming for...
Output from:
dput(read.table("clipboard", sep = "\t", header = TRUE))

structure(list(My.data = structure(c(1L, 13L, 2L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 3L, 1L, 1L, 12L, 1L, 13L, 2L, 2L, 4L, 4L, 
5L, 5L), .Label = c("", "1", "10", "2", "3", "4", "5", "6", "7", 
"8", "9", "Desired long format", "patient"), class = "factor"), 
    X = structure(c(1L, 3L, 4L, 2L, 2L, 4L, 2L, 2L, 2L, 4L, 4L, 
    2L, 1L, 1L, 1L, 1L, 3L, 4L, 4L, 2L, 2L, 2L, 2L), .Label = c("", 
    "F", "gender", "M"), class = "factor"), X.1 = structure(c(1L, 
    10L, 6L, 5L, 9L, 9L, 4L, 8L, 2L, 7L, 3L, 9L, 1L, 1L, 1L, 
    1L, 10L, 6L, 6L, 5L, 5L, 9L, 9L), .Label = c("", "21", "24", 
    "27", "29", "39", "48", "56", "59", "age"), class = "factor"), 
    X.2 = structure(c(1L, 7L, 4L, 4L, 4L, 5L, 5L, 4L, 5L, 6L, 
    5L, 6L, 1L, 1L, 1L, 1L, 8L, 2L, 3L, 2L, 3L, 2L, 3L), .Label = c("", 
    "1", "2", "A", "B", "C", "drug1", "treatment"), class = "factor"), 
    X.3 = structure(c(1L, 15L, 9L, 10L, 7L, 11L, 4L, 8L, 3L, 
    2L, 6L, 5L, 1L, 1L, 1L, 1L, 16L, 12L, 14L, 12L, 13L, 12L, 
    14L), .Label = c("", "110", "136", "140", "145", "191", "38", 
    "71", "83", "84", "96", "A", "B", "D", "dose1", "drug"), class = "factor"), 
    X.4 = structure(c(1L, 9L, 12L, 11L, 10L, 12L, 11L, 12L, 11L, 
    10L, 12L, 11L, 1L, 1L, 1L, 1L, 8L, 6L, 3L, 7L, 2L, 4L, 5L
    ), .Label = c("", "111", "19", "38", "55", "83", "84", "dose", 
    "dosecat1", "high", "low", "med"), class = "factor"), X.5 = structure(c(1L, 
    12L, 5L, 7L, 11L, 6L, 10L, 4L, 9L, 2L, 3L, 8L, 1L, 1L, 1L, 
    1L, 13L, 16L, 16L, 15L, 16L, 14L, 15L), .Label = c("", "01/11/2019", 
    "06/11/2019", "06/12/2019", "09/01/2020", "09/11/2019", "10/11/2019", 
    "12/11/2019", "13/12/2019", "18/11/2019", "29/11/2019", "date1", 
    "dosecat", "high", "low", "med"), class = "factor"), X.6 = structure(c(1L, 
    11L, 9L, 8L, 9L, 8L, 8L, 9L, 8L, 9L, 12L, 12L, 1L, 1L, 1L, 
    1L, 10L, 4L, 2L, 5L, 3L, 7L, 6L), .Label = c("", "01/12/2019", 
    "08/01/2020", "09/01/2020", "10/11/2019", "23/11/2019", "29/11/2019", 
    "B", "D", "date", "drug2", "E"), class = "factor"), X.7 = structure(c(1L, 
    11L, 6L, 2L, 8L, 7L, 9L, 5L, 3L, 10L, 9L, 4L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "111", "147", 
    "148", "170", "19", "28", "55", "56", "75", "dose2"), class = "factor"), 
    X.8 = structure(c(1L, 2L, 5L, 5L, 4L, 4L, 5L, 3L, 3L, 3L, 
    3L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
    "dosecat2", "high", "low", "med"), class = "factor"), X.9 = structure(c(1L, 
    12L, 2L, 4L, 11L, 7L, 8L, 9L, 10L, 5L, 3L, 6L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "01/12/2019", 
    "07/01/2020", "08/01/2020", "09/01/2020", "10/11/2019", "11/01/2020", 
    "15/11/2019", "21/12/2019", "22/11/2019", "23/11/2019", "date2"
    ), class = "factor")), class = "data.frame", row.names = c(NA, 
-23L))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment