Skip to content
Prev 256747 / 398506 Next

Decimals in R/SQL

The problem is that you data is 'integer' and I assume that the
database is keeping everything integer.  You can do what you are
doing, or convert to 'numeric':
+ 33323    1                30                 130
+ 33324    1                10                 186
+ 33325    1                 2                 162
+ 33326    1                80                  93
+ 33327    1                29                 135
+ 33328    1                66                  54
+ 33329    1                62                  54
+ 33330    1                21                 138
+ 33331    1                29                 103
+ 33332    1                 7                 144
+ 33333    1                 5                 143"), header = TRUE)
'data.frame':   11 obs. of  3 variables:
 $ ST   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ AGEP : int  30 10 2 80 29 66 62 21 29 7 ...
 $ PWGTP: int  130 186 162 93 135 54 54 138 103 144 ...
+     select ST, sum(AGEP * PWGTP) / sum(PWGTP)
+         from x
+         group by ST
+ ')
ST sum(AGEP * PWGTP) / sum(PWGTP)
1  1                             23
'data.frame':   11 obs. of  3 variables:
 $ ST   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ AGEP : num  30 10 2 80 29 66 62 21 29 7 ...
 $ PWGTP: int  130 186 162 93 135 54 54 138 103 144 ...
+     select ST, sum(AGEP * PWGTP) / sum(PWGTP)
+         from x
+         group by ST
+ ')
ST sum(AGEP * PWGTP) / sum(PWGTP)
1  1                       23.81446

        
On Wed, Apr 13, 2011 at 12:42 PM, Rachel Licata <RachelL at kff.org> wrote: