[R] thanks -- Re: syntax for identifying more than one

arun smartpink111 at yahoo.com
Sat Dec 29 02:36:22 CET 2012


HI Greg,
Sorry, I misunderstand your question.
I am not sure whether it works with numSummary() from library(Rcmdr). 
You could use other ways, such as:
test<-read.table(text="
 id year incidents
 100    1        0
 101    1        1
 102    1        21
 100    1        27
 101    1        3
 102    1        12
 100    2        5
 101    2        5
 102    2        19
 100    2        10
 101    2        2
 102    2        12
 100    3        0
 101    3        0
 102    3        22
 100    3        14
 101    3        16
 102    3        13
 ",sep="",header=TRUE)

test1<-test
test1<-within(test1,{id<-factor(id);year<-factor(year)})
library(plyr)
ddply(test,.(id,year),summarise,mean=mean(incidents),sd=sd(incidents),n=length(incidents))
#or
by(test1$incidents,list(test1$id,test1$year),FUN=function(x) c(mean=mean(x),sd=sd(x),n=length(x)))
#or
with(test,aggregate(incidents,by=list(id,year),function(x) c(mean=mean(x),sd=sd(x),n=length(x))))
#or
library(data.table)
test1<- data.table(test)
test1[,list(mean=mean(incidents),sd=sd(incidents),n=length(incidents)),by=list(id,year)]

library(psych)
res<-describeBy(test[,3],group=list(test$id,test$year),mat=FALSE)
res
#: 100
#: 1
#  var n mean    sd median trimmed   mad min max range skew kurtosis   se
#1   1 2 13.5 19.09   13.5    13.5 20.02   0  27    27    0    -2.75 13.5
#------------------------------------------------------------ 
#: 101
#: 1
#  var n mean   sd median trimmed  mad min max range skew kurtosis se
1   1 2    2 1.41      2       2 1.48   1   3     2    0    -2.75  1
------------------------------------------------------------------
A.K.





----- Original Message -----
From: "agreg2 at gmail.com" <agreg2 at gmail.com>
To: smartpink111 at yahoo.com
Cc: 
Sent: Friday, December 28, 2012 6:09 PM
Subject: thanks -- Re: syntax for identifying more than one

Hi Arun,
Thanks for responding to my posted question.  Your example showed how to use numsummary to produce mean incidents, sd incidents, etc. for each level of "id" and for each level of "year."  I'm trying to generate these descriptive stats for each *combination* of "id" and "year."  I could do that in SAS with the following code 

proc means mean std n data=test;
  by id year;
  var incidents;
  output out = testsummary
      mean = mnincidents
      std = sdincidents
      n = nincidnets;
proc print data=testsummary;

Again, thanks for responding to the posted question.

Best wishes,
Greg


<quote author='arun kirshna'>
HI,
You could try that in a list if that is okay.
test<-read.table(text="
id year incidents
100    1        0
101    1        1
102    1        21
103    1        27
104    1        3
105    1        12
100    2        5
101    2        5
102    2        19
103    2        10
104    2        2
105    2        12
100    3        0
101    3        0
102    3        22
103    3        14
104    3        16
105    3        13
",sep="",header=TRUE)
library(e1071)
library(Rcmdr)

res<- lapply(seq_len(ncol(test[,-3])),function(i)
numSummary(test[,3],groups=test[,i]))
names(res)<-names(test)[-3]
res
$id
#         mean        sd IQR 0%  25% 50%  75% 100% data:n
#100  1.666667 2.8867513 2.5  0  0.0   0  2.5    5      3
#101  2.000000 2.6457513 2.5  0  0.5   1  3.0    5      3
#102 20.666667 1.5275252 1.5 19 20.0  21 21.5   22      3
#103 17.000000 8.8881944 8.5 10 12.0  14 20.5   27      3
#104  7.000000 7.8102497 7.0  2  2.5   3  9.5   16      3
#105 12.333333 0.5773503 0.5 12 12.0  12 12.5   13      3
#
#$year
#       mean        sd   IQR 0%  25%  50%   75% 100% data:n
#1 10.666667 11.325487 17.25  0 1.50  7.5 18.75   27      6
#2  8.833333  6.177918  6.50  2 5.00  7.5 11.50   19      6
#3 10.833333  8.953584 12.25  0 3.25 13.5 15.50   22      6
A.K.

</quote>
Quoted from: 
http://r.789695.n4.nabble.com/syntax-for-identifying-more-than-one-group-in-numsummary-tp4654172p4654185.html




More information about the R-help mailing list