Re: [R] by function with sum does not give what is expected from by function with print

2020-07-23 Thread Rui Barradas
Hello, These two gives the same results: aggregate(values ~ sex + status, mydata, sum) #  sex status values #1   0  0    224 #2   1  0   5227 #3   0  1 11 #4   1  1    552 by(mydata$values, list(mydata$sex, mydata$status), sum) #: 0 #: 0 #[1] 224

Re: [R] by function with sum does not give what is expected from by function with print

2020-07-23 Thread Rasmus Liland
On 2020-07-24 01:48 +0200, Rasmus Liland wrote: > aggregate(x=list("values"=mydata$values), > by=list("sex"=mydata$sex, > "status"=mydata$status), > FUN=sum) > > yields > > sex status values > 1 0 0224 >

Re: [R] by function with sum does not give what is expected from by function with print

2020-07-23 Thread Rasmus Liland
On 2020-07-23 18:54 -0400, Duncan Murdoch wrote: > On 23/07/2020 6:15 p.m., Sorkin, John wrote: > > Colleagues, > > The by function in the R program below is not giving me the sums > > I expect to see, viz., > > 382+170=552 > > 4730+170=4900 > > 5+6=11 > > 199+25=224 > >

Re: [R] by function with sum does not give what is expected from by function with print

2020-07-23 Thread Duncan Murdoch
On 23/07/2020 6:15 p.m., Sorkin, John wrote: Colleagues, The by function in the R program below is not giving me the sums I expect to see, viz., 382+170=552 4730+170=4900 5+6=11 199+25=224 ### #full R program: mydata <-

Re: [R] by function with sum does not give what is expected from by function with print

2020-07-23 Thread Bert Gunter
by() chooses **data frame** subsets -- sum() is acting on these frames, adding up everything in them. Try this instead: > by(mydata,list(mydata$sex,mydata$status),function(x)sum(x$values)) : 0 : 0 [1] 224 --- : 1 : 0 [1] 5227

[R] by function with sum does not give what is expected from by function with print

2020-07-23 Thread Sorkin, John
Colleagues,   The by function in the R program below is not giving me the sums I expect to see, viz., 382+170=552 4730+170=4900 5+6=11 199+25=224 ### #full R program: mydata <- data.frame(covid=c(0,0,0,0,1,1,1,1), sex=(rep(c(1,1,0,0),2)),