Saturday 15 August 2015

how to reorder my R dataframe by date and filled with NAs? -


I want to change DF 2 from dataframe DF1 like the following:

  df1 & Lt; - Read.table (Text connection (1101 9900104 6.5867 1102 19900105 6.5383 1103 19900106 6.6043 1101 19900106 3.6368 1103 19900107 1.2740 1104 19900107 3.8572 1101 19900106 2.2525 1102 19900107 1.1253 1101 19900107 2.2331 ", Header = T) DF2 & LT ; - read.table (text connection ("1111 1102 1103 1104 19900104 6.5867 NA NA NA 19900105 3.6 9 43 6.5383 NA 19900106 2.2525 3.6368 6.6043 NA 19900107 2.2331 1.1253 1.2740 3.8572"), header = t)   

I tried to use the loop but I do not think this is a good solution, if I have very large data that add up to date from 1990 to 2012. Thanks a lot for me, I can help me ...

It has long been in wide format Is going to be. reshape2 is a great package to work with these types of problems, to go long, you want to use the dcast () . You assign the object to work with it ( df1 ), then a formula, which basically indicates that the rows on the left side of ~ Damage is and what columns are indexed on the right.

  Library (reshape2) df2 & lt; - dcast (df1, date ~ id) df2 # date 1101 1102 1103 1104 # 1 19900104 6.5867 NA NA # 2 19900105 3.6943 6.5383 NA # 3 19900106 2.2525 3.6368 6.6043 NA # 4 19900107 2.2331 1.1253 1.2740 3.8572    

No comments:

Post a Comment