Dear all,
I have a table like this: > eds R.ID Region Gender Agegr Time nvisits 1 1 A F 60--64 1:00 1 2 2 O F 55--59 1:20 1 3 3 O F 55--59 3:45 3 4 4 S M 60--64 1:10 3 5 5 W F 55--59 12:30 1 6 6 W M 60--64 8:00 2 I got a bootstrap sample using the following code: > r<-sample(eds[,1],replace=TRUE) > r [1] 2 4 3 2 6 4 > beds<-eds[r,] > beds R.ID Region Gender Agegr Time nvisits 2 2 O F 55--59 1:20 1 4 4 S M 60--64 1:10 3 3 3 O F 55--59 3:45 3 2.1 2 O F 55--59 1:20 1 6 6 W M 60--64 8:00 2 4.1 4 S M 60--64 1:10 3 I want to sum the last column by columns 2,3,and 4(including 0 in some group). I tried the following codes: #1 : only get the freq, not the sum of the last column. > table<-as.data.frame(with(beds,table(beds[,2],beds[,3],beds[,4]))) > table Var1 Var2 Var3 Freq 1 A F 55--59 0 2 O F 55--59 3 3 S F 55--59 0 4 W F 55--59 0 5 A M 55--59 0 6 O M 55--59 0 7 S M 55--59 0 8 W M 55--59 0 9 A F 60--64 0 10 O F 60--64 0 11 S F 60--64 0 12 W F 60--64 0 13 A M 60--64 0 14 O M 60--64 0 15 S M 60--64 2 16 W M 60--64 1 # 2: only got the sum the last column, but miss the group with 0 counts. > aggregate(beds[,6],list(beds[,2],beds[,3],beds[,4]),sum) Group.1 Group.2 Group.3 x 1 O F 55--59 5 2 S M 60--64 6 3 W M 60--64 2 In conclusion, the following is what I want: Var1 Var2 Var3 Freq 1 A F 55--59 0 2 O F 55--59 5 3 S F 55--59 0 4 W F 55--59 0 5 A M 55--59 0 6 O M 55--59 0 7 S M 55--59 0 8 W M 55--59 0 9 A F 60--64 0 10 O F 60--64 0 11 S F 60--64 0 12 W F 60--64 0 13 A M 60--64 0 14 O M 60--64 0 15 S M 60--64 6 16 W M 60--64 2 Does anyone know a code to do this or give a hint? Thank you in advance. Betty [[alternative HTML version deleted]] ______________________________________________ R-help@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.