1

There are total 17 dataframe and I have them stored in a list:

req_dfs

"data_Apr-18" "data_Apr-19" "data_Aug-18" "data_Aug-19" "data_Dec-18" "data_Feb-19" "data_Jan-19" "data_Jul-18" "data_Jul-19" "data_Jun-18" "data_Jun-19" "data_Mar-19" "data_May-18" "data_May-19" "data_Nov-18" "data_Oct-18" "data_Sep-18"

when I wrote a command to return the melt dataframe I got the desired results as I was passing only a single dataframe i.e.,

nec_col <-c("ID","Code","Name","Last Files Processed Date","Last Report Sent Date")
melt_data <- melt(`data_Apr-18`,id.vars = nec_col)

I get the desired outcome:

melt_data 

    ID      Code           Name  Last Files Processed Date  Last Report Sent Date   Variable     Value
    3498    000199~3498    H1          30/7/2019                   31/7/2019        1-Apr-19      DONE
    3343    000225~3343    H2          27/6/2018                   28/6/2018        1-Apr-19    NOTEXT
    2117    000279~2117    H3          31/7/2019                   1/8/2019         1-Apr-19    DONE
    184     001888         H4          NA                          NA               1-Apr-19    NOTEXT
    2576    0037811~2576   H5          NA                          NA               1-Apr-19    NOTEXT
    3291    003929~3291    H6          2/10/2018                   3/10/2018        1-Apr-19    NOTEXT
    3497    004434~3497    H7          30/7/2019                   31/7/2019        1-Apr-19    NOTEXT
.                       
.                       
.                       
.                       
.                       
.                       
.                       
    3497    004434~3497 H7  30/7/2019       31/7/2019       3-Apr-19    NOTEXT

I wrote a loop to perform melt on each and every dataframe and then perform rbind.

for(i in req_dfs)
{

  nec_col <-c("ID","Code","Name","Last Files Processed Date","Last Report Sent Date")

  melt_data <- melt(i,id.vars = nec_col)

  melt_final <- rbind(melt_final,melt_data)

  print(paste0("finished processing: ", i))

}

This loop is returning unexpected results:

data_Apr-18
data_Apr-19
data_Aug-18
data_Aug-19
data_Dec-18
data_Feb-19
data_Jan-19
data_Jul-18
data_Jul-19
data_Jun-18
data_Jun-19
data_Mar-19
data_May-18
data_May-19
data_Nov-18
data_Oct-18
data_Sep-18

I don't know how to pass the data frame to the melt function used in the loop. It could be a simple problem but for the past 3 hours I've been working on that but din't go anywhere. Any help is highly appreciated.

sample data for one dataframe data_Apr-18:

ID       Code           Name    Last File Processed Last report Sent   1-Apr-19 2-Apr-19    3-Apr-19
3498    000199~3498     H1           30/7/2019        31/7/2019     DONE DONE   DONE    
3343    000225~3343     H2           27/6/2018        28/6/2018 NOTEXT  NOTEXT  NOTEXT
2117    000279~2117     H3           31/7/2019        1/8/2019  NOTEXT  DONE    DONE
184     001888          H4           NA               NA    NOTEXT  NOTEXT  DONE
2576    0037811~2576    H5           NA               NA    NOTEXT  NOTEXT  DONE
3291    003929~3291     H6           2/10/2018        3/10/2018 NOTEXT  NOTEXT  NOTEXT
3497    004434~3497     H7           30/7/2019        31/7/2019 NOTEXT  DONE    NOTEXT

Just in case if you don't understand the sample data I'm attaching a picture Sample Data

Toros91
  • 303
  • 2
  • 11

1 Answers1

3

I think what you have in req_dfs are just names of dataframes, you can use mget to get them as list and then use lapply to loop over each and melt it into long format and use rbindlist to bind all the data in one dataframe.

library(data.table)
rbindlist(lapply(mget(req_dfs), melt, id.vars = nec_col))

Or if you want to use tidyverse, we can use map_df/map_dfr along with gather

library(tidyverse)
map_df(mget(req_dfs),~gather(., key, value, -nec_col))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213