Search code examples
rmeltmeasure

How to use vector of strings in measure.vars in melt in R?


I have a data.csv file as:

BBBB,  B, OOB 

CCCC,  C, OOC

DDDD,  D, OOD

EEEE,  E, OOE

I obtained the 2nd column as:

df1 <- read.csv(data, header=FALSE,strip.white=TRUE,stringsAsFactors=FALSE)[2].

The question is how can I use the df1 as c("B", "C", "D", "E)" in the melt function to use the df1 as measure.vars for a different set of data (data2 for example which has headers as B,C,D,E).

B,C,D,E

9.43,9.49,9.61,9.04


7.01,3.43,3.63,3.55

10.35,9.05,9.49,8.45

4.83,1.89,1.79,1.94

10.3,10.39,9.67,8.95

I wanted to use as:

df2 = data.frame(melt(data2, measure.vars=df1, variable.name=xxxx, value.name="yyyy"), m="zzzz")

Solution

  • If we use [ without any ,, then it would be still a data.frame with a single column (assuming the original dataset is data.frame. Better option is [[ to extract as a vector and the measure argument expects a vector. It is also better to wrap with unique (in case there are duplicates)

    v1 <- unique(read.csv('file.csv', header=FALSE,strip.white=TRUE,stringsAsFactors=FALSE)[[2]])
    

    Now, we use that in measure

    library(reshape2)
    df2 <- data.frame(melt(data2, measure.vars=v1, variable.name='xxxx', 
             value.name="yyyy"), m="zzzz")