myvar <- as.data.frame(rbind(c("Walter","NA","NA","NA","NA"), c("Walter","NA","NA","NA","NA"), c("Walter","Jesse","NA","NA","NA"), c("Gus","Tuco","Mike","NA","NA"), c("Gus","Mike","Hank","Saul","Flynn"))) ID <- as.factor(c(1:5)) df <- data.frame(ID, myvar)
Using basic adjustment. (I am converting your "NA" character strings to NA , which you may not have to do, this is only related to how you created this example)
df[df == 'NA'] <- NA na.omit(reshape(df, direction = 'long', varying = list(2:6))[, c('ID','V1')]) # ID V1 # 1.1 1 Walter # 2.1 2 Walter # 3.1 3 Walter # 4.1 4 Gus # 5.1 5 Gus # 3.2 3 Jesse # 4.2 4 Tuco # 5.2 5 Mike # 4.3 4 Mike # 5.3 5 Hank # 5.4 5 Saul # 5.5 5 Flynn
or using reshape2
library('reshape2')
you receive warnings that the factor levels in the columns do not match, but that itβs fine.
source share