R how to add and save coefficient

Question: How can I add two or more columns of numbers and , to preserve the coefficient

I have data.frame :

 patient analyte1value analyte2value analyte3value pt1 1 3 5 pt2 2 6 7 pt3 9 10 2 ... 

I know I can use stack(select=c(columnnames)) , but I am losing the patient factor.

I want to go out:

 pt1 1 analyte1 pt1 3 analyte2 pt1 5 analyte3 pt2 2 analyte1 pt2 6 analyte2 ... 

(I have a suspicious suspicion that I need plyr or something like that ...)

thanks.

+4
source share
2 answers

One option is one of the other Hadley packages: reshape2 :

 > require(reshape2) > dat patient analyte1 analyte2 analyte3 1 pt1 1 3 5 2 pt2 2 6 7 3 pt3 9 10 2 > melt(dat, id = "patient") patient variable value 1 pt1 analyte1 1 2 pt2 analyte1 2 3 pt3 analyte1 9 4 pt1 analyte2 3 5 pt2 analyte2 6 6 pt3 analyte2 10 7 pt1 analyte3 5 8 pt2 analyte3 7 9 pt3 analyte3 2 > str(melt(dat, id = "patient")) 'data.frame': 9 obs. of 3 variables: $ patient : Factor w/ 3 levels "pt1","pt2","pt3": 1 2 3 1 2 3 1 2 3 $ variable: Factor w/ 3 levels "analyte1","analyte2",..: 1 1 1 2 2 2 3 3 3 $ value : int 1 2 9 3 6 10 5 7 2 

You can do this in a longer way using reshape() from the R base:

 reshape(dat, direction = "long", sep = "", varying = 2:4, times = names(dat)[2:4], idvar = "patient", timevar = "variable", v.names = "value") 

and the main difference is that variable not a factor with the reshape() base. I suppose this is a user reshape2 , it was the motivation for writing reshape2 ...

+8
source

If I understood correctly, you want to reshape your dataframe to a long format.

 reshape(df,varying=list(2:4),times=names(df)[2:4], idvar="patient",v.names="value",direction="long") 
+2
source

Source: https://habr.com/ru/post/1340656/


All Articles