Re : Manipulating Data Frames

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re : Manipulating Data Frames

mohan L
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Re : Manipulating Data Frames

Henrique Dallazuanna
Try this:

(data[,-(1:2)] - data[,2]) * prop.table(data[,2]) + data[,2]


On Thu, May 20, 2010 at 7:38 AM, Mohan L <[hidden email]> 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...
>
>
>  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-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Re : Manipulating Data Frames

David Winsemius
In reply to this post by mohan L

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]]
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.