[R] Re : Manipulating Data Frames
David Winsemius
dwinsemius at comcast.net
Thu May 20 15:04:55 CEST 2010
On May 20, 2010, at 6:38 AM, Mohan L wrote:
> 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...
data[, 3:7]*(
data[ , 2]/sum(data[ , 2]) )
Gives the reweighted estimates. You could easily cbind them to data[ ,
1:2]
>
>
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list