Hi all, I want create a frequency table using this : xc1<- sample(c(1:10), 100, replace = TRUE) xc2<- sample(c(0,1), 100, replace = TRUE) xc3<- cbind(xc1,xc2) tab1<- xc3[,list( d1=sum(xc2==0), d2=sum(xc2==1)),by=xc1] but not working. Error in `[.data.frame`(xc3, , list(d1 = sum(xc2 == 1), d2 = sum(xc2 == : unused argument (by = xc1) any idea? I want the result like this xc1 d1 d2 1 11 5 2 5 4 3 5 4 4 2 5 5 2 7 6 7 4 7 9 5 8 2 6 9 5 4 10 3 5 [[alternative HTML version deleted]]
Hi Val, Does this help: library(plyr) ddply(as.data.frame(xc3), .variables = "xc1", summarise, d1 = sum(xc2 =0), d2 = sum(xc2 == 1)) You could also try aggregate(xc3, by = list(xc1, xc2), FUN = sum) and modify the output. Best, Ulrik On Thu, 11 Feb 2016 at 04:12 Val <valkremk at gmail.com> wrote:> Hi all, > > I want create a frequency table using this : > > xc1<- sample(c(1:10), 100, replace = TRUE) > xc2<- sample(c(0,1), 100, replace = TRUE) > > xc3<- cbind(xc1,xc2) > > tab1<- xc3[,list( d1=sum(xc2==0), d2=sum(xc2==1)),by=xc1] > > but not working. > > Error in `[.data.frame`(xc3, , list(d1 = sum(xc2 == 1), d2 = sum(xc2 == : > unused argument (by = xc1) > > any idea? > > > > I want the result like this > > xc1 d1 d2 > 1 11 5 > 2 5 4 > 3 5 4 > 4 2 5 > 5 2 7 > 6 7 4 > 7 9 5 > 8 2 6 > 9 5 4 > 10 3 5 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
> On Feb 10, 2016, at 7:10 PM, Val <valkremk at gmail.com> wrote: > > Hi all, > > I want create a frequency table using this : > > xc1<- sample(c(1:10), 100, replace = TRUE) > xc2<- sample(c(0,1), 100, replace = TRUE) > > xc3<- cbind(xc1,xc2) > > tab1<- xc3[,list( d1=sum(xc2==0), d2=sum(xc2==1)),by=xc1] > > but not working. >"Not working." Sadder and less specific complaints were never heard. You are not saying what the goal of this effort might be. Perhaps:> table(xc1=xc3[,1], d=xc3[,2])d xc1 0 1 1 5 4 2 5 5 3 6 3 4 1 5 5 4 6 6 6 8 7 10 5 8 4 6 9 8 3 10 2 4 Furthermore, your syntax uses suggest you may be expecting us all to have data.table loaded, .... not a reasonable expectation. One which you should be stating explicitly with a library or require call.> Error in `[.data.frame`(xc3, , list(d1 = sum(xc2 == 1), d2 = sum(xc2 == : > unused argument (by = xc1) > > any idea? > > > > I want the result like thisWell, the resutl from `table` does resemble this, but since the goal was never articulated, I'm unsure if it is a success.> > xc1 d1 d2 > 1 11 5 > 2 5 4 > 3 5 4 > 4 2 5 > 5 2 7 > 6 7 4 > 7 9 5 > 8 2 6 > 9 5 4 > 10 3 5 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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 Alameda, CA, USA