-2

I have a dataframe (DF_melted) which I obtained by melting some other dataset. The DF_melted dataframe has columns "month","A","B","C","D","E","F". From the following code using dcast, I am able to get a dataframe which contains value of mean of the variable for each combination of "A" and "month". This all works fine and as expected.

dcast_data<-dcast(DF_Melted,
              month+A~variable,
              fun.aggregate = mean)

Question- On lines of the above code, I want to run a for loop to automatically obtain the dataset (using dcast) for relationship of month+A, month+B, month+C, month+D . I am unable to figure out about how to substitute 'A' (or B, C, D) in a paremetric manner.

I tried the following code where I reference to A,B,C,D as per their column number in DF_melted and it works:

for(j in seq(2,5, by=1))  #'A' is 2nd column, 'D' is 5th column
{
  dcast_data<-dcast(DF_Melted,
              month+DF_Melted[,j]~variable,
              fun.aggregate = mean)      
  FinalDF<-cbind(FinalDF,dcast_data)
}

Although the above works, I am wondering if there is a smarter way to do the above without referencing the column number of the data frame?

Eventually my intention is to get a dataframe 'FinalDF' so that I could use it to plot the month v/s variable graph for each category of A, B, C, D. So doing this data reshaping automatically would be an immense help.

Sinha
  • 431
  • 1
  • 5
  • 12
  • Please show a small reproducible example and expected output. Based on the info, you may also `melt` the 'A/B/C/D' columns into a single column and then do the `dcast` – akrun Oct 02 '16 at 18:37
  • The dataset is a movie database with rows as the movie names. A,B,C,D names 'action','drama','comedy','thriller' and their values are 1 or 0. So if movie in row 45 is action and comedy, both of these columns will have entries '1'. 'Variable' in the melted dataframe is revenue in $ The dataframe is too large (and has much more genres) to provide an example. Extremely sorry for not providing it. I am sure there is an easy way to dsplay it but am new to R and am not sure how to do so. – Sinha Oct 02 '16 at 19:50
  • you can refer [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – akrun Oct 02 '16 at 19:56

1 Answers1

0

Consider a do.call(cbind, dfList) on a list of melted data frames:

dfList <- lapply(c("A", "B", "C", "D"), function(i) {
    dcast(DF_Melted, month+DF_Melted[i]~variable, 
          fun.aggregate = mean)
})

FinalDF <- do.call(cbind, dfList)
Parfait
  • 104,375
  • 17
  • 94
  • 125