hi, i want to use the function table to build a table not of frequence (number of time the vareable is repeated in a list or a data frame!!) but in function of classes I don t find a clear explnation in examples of ?table !!! example x y z 1 0 100 5 1 1500 6 1 1200 2 2 500 1 1 3500 5 2 2000 8 5 4500 i want to do a table summerizing the number of variable where z is in [0-1000],],[1000-3000], [> 3000] thank you very much for your help [[alternative HTML version deleted]]
You need to create a factor that indicates which group the values in 'z' belong to. The easiest way to do that based on your situation is to use the 'cut' function to construct the factor, and then call 'table' using the result created by 'cut'. See ?cut and ?factor -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Inchallah Yarab Sent: Monday, August 24, 2009 10:59 AM To: r-help at r-project.org Subject: [R] table function hi, i want to use the function table to build a table not of frequence (number of time the vareable is repeated in a list or a data frame!!) but in function of classes I?don t find?a clear explnation in? examples of ??table !!! example x?? ?? y??????? z 1??? 0?????? 100 5??? 1?????? 1500 6??? 1?????? 1200 2??? 2?????? 500 1??? 1?????? 3500 5???? 2???? 2000 8???? 5???? 4500 i want to do a table summerizing the number of variable where z is in [0-1000],],[1000-3000], [> 3000] thank you very much for your help [[alternative HTML version deleted]]
Inchallah Yarab wrote:> > i want to do a table summerizing the number of variable where z is in > [0-1000],],[1000-3000], [> 3000] >You can use "cut" to create a new vector of labels and tabulate the result. Options control closed/open endpoints (see ?cut):> z <- c(100,1500,1200,500,3500,2000,4500)> table(cut(z,c(0,1000,3000,max(z))))(0,1e+03] (1e+03,3e+03] (3e+03,4.5e+03] 2 3 2 -- View this message in context: http://www.nabble.com/table-function-tp25118909p25119226.html Sent from the R help mailing list archive at Nabble.com.
On Aug 24, 2009, at 10:59 AM, Inchallah Yarab wrote:> hi, > > i want to use the function table to build a table not of frequence > (number of time the vareable is repeated in a list or a data > frame!!) but in function of classes > I don t find a clear explnation in examples of ?table !!! > > example > > x y z > 1 0 100 > 5 1 1500 > 6 1 1200 > 2 2 500 > 1 1 3500 > 5 2 2000 > 8 5 4500 > > i want to do a table summerizing the number of variable where z is > in [0-1000],],[1000-3000], [> 3000] > > thank you very much for your helpSee ?cut, which bins a continuous variable. > DF x y z 1 1 0 100 2 5 1 1500 3 6 1 1200 4 2 2 500 5 1 1 3500 6 5 2 2000 7 8 5 4500 > table(cut(DF$z, breaks = c(-Inf, 1000, 3000, Inf), labels = c("0 - 1000", ">1000 - 3000", ">3000"))) 0 - 1000 >1000 - 3000 >3000 2 3 2 HTH, Marc Schwartz
Your question is a little vague. Do you just want to know how often z falls in one the three classes? If so, you could either code an indicator variable (e.g. z.cat) that expresses the three categories and then do table(z.cat). Alternatively, you could just do sum(z>0&z<1000) sum(z>10000&z<3000) sum(z>3000) It's unclear to me whether you want to summarize the other variables (x and y) for each of the categories of z. If you want to do that, use tapply (see ?tapply). Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von Inchallah Yarab Gesendet: Monday, August 24, 2009 11:59 AM An: r-help at r-project.org Betreff: [R] table function hi, i want to use the function table to build a table not of frequence (number of time the vareable is repeated in a list or a data frame!!) but in function of classes I don t find a clear explnation in examples of ?table !!! example x y z 1 0 100 5 1 1500 6 1 1200 2 2 500 1 1 3500 5 2 2000 8 5 4500 i want to do a table summerizing the number of variable where z is in [0-1000],],[1000-3000], [> 3000] thank you very much for your help [[alternative HTML version deleted]]