Dear all, ftable produces a list of the frequencies of all occuring values. But how about the occuring values? How can I retrieve a list of occuring values? How can I retrieve a table with both the list of occuring values and their respective frequencies? Thank you in advance, Yours, Ferri
Your question does not make sense. Show us what you have tried. If you cannot include part of the data you are using, try the Titanic data set included with R:> data(Titanic) > str(Titanic)table [1:4, 1:2, 1:2, 1:2] 0 0 35 0 0 0 17 0 118 154 ... - attr(*, "dimnames")=List of 4 ..$ Class : chr [1:4] "1st" "2nd" "3rd" "Crew" ..$ Sex : chr [1:2] "Male" "Female" ..$ Age : chr [1:2] "Child" "Adult" ..$ Survived: chr [1:2] "No" "Yes"> ftable(Titanic, row.vars=c(1, 4), colvars=c(2, 3))Sex Male Female Age Child Adult Child Adult Class Survived 1st No 0 118 0 4 Yes 5 57 1 140 2nd No 0 154 0 13 Yes 11 14 13 80 3rd No 35 387 17 89 Yes 13 75 14 76 Crew No 0 670 0 3 Yes 0 192 0 20 ---------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77843-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ferri Leberl Sent: Thursday, September 21, 2017 8:01 AM To: r-help at r-project.org Subject: [R] List of occuring values Dear all, ftable produces a list of the frequencies of all occuring values. But how about the occuring values? How can I retrieve a list of occuring values? How can I retrieve a table with both the list of occuring values and their respective frequencies? Thank you in advance, Yours, Ferri ______________________________________________ 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.
unique(x) will give you the distinct values in x. table(x) will give you the distrinct values and their frequencies as an array with dimnames. data.frame(table(x)) will give you a 2-column data.frame with the distinct values and their frequencies.> values <- c("Small", "Large", "Large", "Large") > unique(values)[1] "Small" "Large"> tblValues <- table(values) > tblValuesvalues Large Small 3 1> tblValues[tblValues > 2, drop=FALSE]values Large 3> > dfValues <- data.frame(tblValues) > dfValuesvalues Freq 1 Large 3 2 Small 1> subset(dfValues, Freq > 2)values Freq 1 Large 3> > factorValues <- factor(values,levels=c("Small","Medium","Large","XLarge"))> data.frame(table(factorValues))factorValues Freq 1 Small 1 2 Medium 0 3 Large 3 4 XLarge 0 Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Sep 21, 2017 at 6:01 AM, Ferri Leberl <ferri.leberl at gmx.at> wrote:> > Dear all, > ftable produces a list of the frequencies of all occuring values. > But how about the occuring values? > How can I retrieve a list of occuring values? > How can I retrieve a table with both the list of occuring values and their > respective frequencies? > Thank you in advance, > Yours, Ferri > > ______________________________________________ > 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]]
Note that this data.frame(table(...)) makes a column for each argument to table(...), plus a column for the frequencies so you can easily deal with multiway tabulations.> rawData <- data.frame(+ sizes = c("Small", "Large", "Large", "Large"), + colors = c("Red", "Blue", "Blue", "Red"), + condition = c("Good", "Poor", "Poor", "Poor"))> data.frame(with(rawData, table(sizes, colors, condition)))sizes colors condition Freq 1 Large Blue Good 0 2 Small Blue Good 0 3 Large Red Good 0 4 Small Red Good 1 5 Large Blue Poor 2 6 Small Blue Poor 0 7 Large Red Poor 1 8 Small Red Poor 0> subset(.Last.value, Freq>0)sizes colors condition Freq 4 Small Red Good 1 5 Large Blue Poor 2 7 Large Red Poor 1 Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Sep 21, 2017 at 7:38 AM, William Dunlap <wdunlap at tibco.com> wrote:> unique(x) will give you the distinct values in x. table(x) will give you > the distrinct values and their frequencies as an array with dimnames. > data.frame(table(x)) will give you a 2-column data.frame with the distinct > values and their frequencies. > > > values <- c("Small", "Large", "Large", "Large") > > unique(values) > [1] "Small" "Large" > > tblValues <- table(values) > > tblValues > values > Large Small > 3 1 > > tblValues[tblValues > 2, drop=FALSE] > values > Large > 3 > > > > dfValues <- data.frame(tblValues) > > dfValues > values Freq > 1 Large 3 > 2 Small 1 > > subset(dfValues, Freq > 2) > values Freq > 1 Large 3 > > > > factorValues <- factor(values, levels=c("Small","Medium"," > Large","XLarge")) > > data.frame(table(factorValues)) > factorValues Freq > 1 Small 1 > 2 Medium 0 > 3 Large 3 > 4 XLarge 0 > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Thu, Sep 21, 2017 at 6:01 AM, Ferri Leberl <ferri.leberl at gmx.at> wrote: > >> >> Dear all, >> ftable produces a list of the frequencies of all occuring values. >> But how about the occuring values? >> How can I retrieve a list of occuring values? >> How can I retrieve a table with both the list of occuring values and >> their respective frequencies? >> Thank you in advance, >> Yours, Ferri >> >> ______________________________________________ >> 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/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > >[[alternative HTML version deleted]]
Hi Ferri, Do you mean getting something like the vector of original values back? boodat<-sample(LETTERS[1:4],100,TRUE) bootab<-table(boodat) untab<-function(x) { lenx<-length(x) newx<-NULL for(i in 1:lenx) newx<-c(newx,rep(names(x)[i],x[i])) return(newx) } untab(bootab) Jim On Thu, Sep 21, 2017 at 11:01 PM, Ferri Leberl <ferri.leberl at gmx.at> wrote:> > Dear all, > ftable produces a list of the frequencies of all occuring values. > But how about the occuring values? > How can I retrieve a list of occuring values? > How can I retrieve a table with both the list of occuring values and their respective frequencies? > Thank you in advance, > Yours, Ferri > > ______________________________________________ > 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.