r - converting a dataframe to a 2 column dataframe based on vaues -
i trying convert multi-column dataframe 2 column dataframe. each row person (based on id in first col) multiple attributes. how can list each person , attribute in 2 columns?
example:
temp<-data.frame(v1 = c(1,2,3),v2 = c(4,5,6)) row.names(temp)<-c("person1","person2","person3") temp v1 v2 person1 1 4 person2 2 5 person3 3 6 res<-data.frame(person=c("person1","person1","person2","person2","person3", "person3"),val= c(1,4,2,5,3,6)) res person val 1 person1 1 2 person1 4 3 person2 2 4 person2 5 5 person3 3 6 person3 6
here's comment converted answer per request
library(reshape2) temp$id <- rownames(temp) melt(temp, "id")
or simpler version @akrun
melt(as.matrix(temp))
Comments
Post a Comment