# Re : Manipulating Data Frames

3 messages
Open this post in threaded view
|

## Re : Manipulating Data Frames

 Dear All, I have data some thisng like this : > data <- read.csv(file='ipsample.csv',sep=',' , header=TRUE) > data   State  Jan  Feb  Mar  Apr  May Jun 1   AAA    1    1    0    2    2   0 2   BBB 1298 1195 1212 1244 1158 845 3  CCC     0    0    0    1    2   1 4   DDD    5   11   17   15   10   9 5   EEE   18   28   27   23   23  16 6   FFF   68  152  184  135  111  86 from this data frame, I took "Jan" as base and calculating weightage like this : > basemonth.sum <- sum(data[[2]]) > basemonth.sum [1] 1390 > basemonth.data <- data[[2]] >  basemonth.data [1]    1 1298    0    5   18   68 > weightage <- basemonth.data / basemonth.sum > weightage [1] 0.0007194245 0.9338129496 0.0000000000 0.0035971223 0.0129496403 [6] 0.0489208633 The above is the weightage for base month "Jan".  Now I need to calculate weighted states data. What I need to do is : (((Feb[i]-Jan[1])*weightage)+Jan[1]) for all column. The "Jan" column is fixed. I need to do the calculation in all the column Feb, Mar etc...  State    Jan   Feb         Mar 1   AAA    1    (((Feb[1]-Jan[1])*weightage[1])+Jan[1]) (((Mar[1]-Jan[1])*weightage[1])+Jan[1]) 2   BBB 1298 (((Feb[2]-Jan[2])*weightage[2])+Jan[2]) (((Mar[2]-Jan[1])*weightage[2])+Ja[1]) 3   CCC     0 4   DDD    5 5   EEE   18 6   FFF   68 I am struggling with this . I have framed a logic using for loop. But it seems me very bad logic.  Any help will be greatly appreciated. Thanks & Rg Mohan L         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.