1

I have a dataset with the following format:

name1 year name2 profits2010 profits2009 count 
AA    2009  AA    10           15          20
AA    2010  AA    10           15          3
BB    2009  BB     4           NA          34
BB    2010  BB     4           NA          4

I need to reshape the data to this format.Any ideas on how this can be done?

name1 year name2 profits count
AA    2009 AA     15       20
AA    2010 AA     10       3
BB    2009 BB     NA       34
BB    2010 BB     4      4
user3570187
  • 1,743
  • 3
  • 17
  • 34

2 Answers2

0

This isn't really reshaping, just defining a new variable. Try this:

df$profits <- ifelse(df$year==2009,df$profits2009,df$profits2010)
jrdnmdhl
  • 1,935
  • 18
  • 26
  • I have like 8 years data. will the code be the same – user3570187 May 26 '15 at 17:17
  • @akrun Why would it be done that way? Second argument of ifelse is the true value and the third is the false value. If the year is 2009, it should grab the 2009 Value. – jrdnmdhl May 26 '15 at 17:42
  • @user3570187 if there are more years then you will need something a bit more complicated, but you can probably just nest several ifelse statements. – jrdnmdhl May 26 '15 at 17:44
  • @jrdnmdhl OP changed the expected output and still I think it is not the correct. We may need to wait to find what the true expected value – akrun May 26 '15 at 17:44
  • My code will assign 2009 values to 2009 year etc... as long as that is the intended functionality I believe my code works. If OP is trying to do something else then it won't though. – jrdnmdhl May 26 '15 at 17:46
0

Try

 indx <- grep('profits', names(df1))
 indx2 <- cbind(1:nrow(df1), match(df1$year, 
               as.numeric(sub('\\D+', '',   names(df1)[indx]))))
 df1$profits <- df1[indx][indx2]
 df1[-indx]
 #   name1 year name2 count profits
 #1    AA 2009    AA    20      15
 #2    AA 2010    AA     3      10
 #3    BB 2009    BB    34      NA
 #4    BB 2010    BB     4       4
akrun
  • 874,273
  • 37
  • 540
  • 662