-2

I have a data frame as below

date_1    Value_1  date_2   Value_2  date_3     Value_3  .  date_100 value_100    
01Mar2010  20     02Mar2011   30    02Jun2001    20      .  03Apr2015  200
03Jun2015  10     04Jun2011   40    30Dec2010    40      .  18Jul2016  10
01Feb2014  50     12Feb2011   60    18Aug2011    50      .  30Mar2016  22

Any Automated codes to get as below output data frame

date       Value  name
01Mar2010    20   Value_1
03Jun2015    10   Value_1
01Feb2014    50   Value_1
02Mar2011    30   Value_2
04Jun2011    40   Value_2
12Feb2011    60   Value_2
02Jun2001    20   Value_3
30Dec2010    40   Value_3
18Aug2011    50   Value_3
    .        .      .
    .        .      .
    .        .      .
03Apr2015   200   Value_100
18Jul2016   10    Value_100
30Mar2016   22    Value_100

Thanks in advance.

digEmAll
  • 56,430
  • 9
  • 115
  • 140
su919
  • 87
  • 8
  • 1
    Please study the [stackoverflow tour](http://stackoverflow.com/tour) on how to ask good questions. Do not post in comments, use edit. Use syntax to highlight code. Many options to consider. – Roman Luštrik Jul 27 '16 at 07:07
  • 1
    There is also [how to make a great reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) thread. Don't forget to show your efforts. – Roman Luštrik Jul 27 '16 at 07:08

1 Answers1

1

We can use melt from data.table which can take multiple measure patterns

library(data.table)
melt(setDT(df1), measure = patterns("^date", "^Value"),
         value.name = c("date", "Value"), variable.name = "name")[,
           name:= paste0("Value_",  unique(sub(".*_", "", names(df1)))[name])][]
#           name      date Value
# 1:   Value_1 01Mar2010    20
# 2:   Value_1 03Jun2015    10
# 3:   Value_1 01Feb2014    50
# 4:   Value_2 02Mar2011    30
# 5:   Value_2 04Jun2011    40
# 6:   Value_2 12Feb2011    60
# 7:   Value_3 02Jun2001    20
# 8:   Value_3 30Dec2010    40
# 9:   Value_3 18Aug2011    50
#10: Value_100 03Apr2015   200
#11: Value_100 18Jul2016    10
#12: Value_100 30Mar2016    22

data

df1 <- structure(list(date_1 = c("01Mar2010", "03Jun2015", "01Feb2014"
), Value_1 = c(20L, 10L, 50L), date_2 = c("02Mar2011", "04Jun2011", 
"12Feb2011"), Value_2 = c(30L, 40L, 60L), date_3 = c("02Jun2001", 
"30Dec2010", "18Aug2011"), Value_3 = c(20L, 40L, 50L),
date_100 = c("03Apr2015", 
"18Jul2016", "30Mar2016"), Value_100 = c(200L, 10L, 22L)),
 .Names = c("date_1", 
 "Value_1", "date_2", "Value_2", "date_3", "Value_3", "date_100", 
 "Value_100"), class = "data.frame", row.names = c(NA, -3L))
akrun
  • 874,273
  • 37
  • 540
  • 662