Hello, Let's assume I have a vector of integers : > myvector <- c(1, 2, 3, 2, 1, 3, 5) My purpose is to obtain the cumulative distribution of these numerical data, i.e. something like : value nb_occur. <=1 2 <=2 4 <=3 6 <=4 6 <=5 7 For this, I create a table with ; > mytable <- table(myvector) 1 2 3 5 2 2 2 1 However, table() returns an array of integers, mytable[4] returns the occurence number of the "5" item, which makes this table hard to index. I would prefer to have a data structure where mytable[4] could return 0, as there is no "4" in my vector. table() may not be the proper way to do it. For the moment, I use a loop which scans the vector from its lowest value to its highest, and counts the number of times each value appears. Is there a built-in function, or a table() option to do such a task ? Thanks. Regards, Nicolas
> myvector <- c(1, 2, 3, 2, 1, 3, 5)> myf <- factor(myvector, levels=1:5) > table(myf) myf 1 2 3 4 5 2 2 2 0 1 > cumsum(table(myf)) 1 2 3 4 5 2 4 6 6 7 Nicolas Perot a ?crit :>Hello, > >Let's assume I have a vector of integers : > > myvector <- c(1, 2, 3, 2, 1, 3, 5) > >My purpose is to obtain the cumulative distribution of these numerical >data, i.e. something like : > >value nb_occur. ><=1 2 ><=2 4 ><=3 6 ><=4 6 ><=5 7 > >For this, I create a table with ; > > mytable <- table(myvector) > >1 2 3 5 >2 2 2 1 > >However, table() returns an array of integers, mytable[4] returns the >occurence number of the "5" item, which makes this table hard to index. >I would prefer to have a data structure where mytable[4] could return 0, >as there is no "4" in my vector. > >table() may not be the proper way to do it. > >For the moment, I use a loop which scans the vector from its lowest >value to its highest, and counts the number of times each value appears. > >Is there a built-in function, or a table() option to do such a task ? > >Thanks. > >Regards, >Nicolas > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > >
something like: cumsum(table(factor(myvector,levels=1:5))) will do the job. Nicolas Perot wrote:>Hello, > >Let's assume I have a vector of integers : > > myvector <- c(1, 2, 3, 2, 1, 3, 5) > >My purpose is to obtain the cumulative distribution of these numerical >data, i.e. something like : > >value nb_occur. ><=1 2 ><=2 4 ><=3 6 ><=4 6 ><=5 7 > >For this, I create a table with ; > > mytable <- table(myvector) > >1 2 3 5 >2 2 2 1 > >However, table() returns an array of integers, mytable[4] returns the >occurence number of the "5" item, which makes this table hard to index. >I would prefer to have a data structure where mytable[4] could return 0, >as there is no "4" in my vector. > >table() may not be the proper way to do it. > >For the moment, I use a loop which scans the vector from its lowest >value to its highest, and counts the number of times each value appears. > >Is there a built-in function, or a table() option to do such a task ? > >Thanks. > >Regards, >Nicolas > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > > >-- St?phane DRAY (dray at biomserv.univ-lyon1.fr ) Laboratoire BBE-CNRS-UMR-5558, Univ. C. Bernard - Lyon I 43, Bd du 11 Novembre 1918, 69622 Villeurbanne Cedex, France Tel: 33 4 72 43 27 57 Fax: 33 4 72 43 13 88 http://www.steph280.freesurf.fr/
I think ecdf() is more appropriate than table() here, e.g., x <- c(1, 2, 3, 2, 1, 3, 5) Fn <- ecdf(x) Fn(1:5) * length(x) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Nicolas Perot" <np at alambic.org> To: <r-help at stat.math.ethz.ch> Sent: Monday, March 06, 2006 10:56 AM Subject: [R] Contingency table and zeros> Hello, > > Let's assume I have a vector of integers : > > myvector <- c(1, 2, 3, 2, 1, 3, 5) > > My purpose is to obtain the cumulative distribution of these > numerical > data, i.e. something like : > > value nb_occur. > <=1 2 > <=2 4 > <=3 6 > <=4 6 > <=5 7 > > For this, I create a table with ; > > mytable <- table(myvector) > > 1 2 3 5 > 2 2 2 1 > > However, table() returns an array of integers, mytable[4] returns > the > occurence number of the "5" item, which makes this table hard to > index. > I would prefer to have a data structure where mytable[4] could > return 0, > as there is no "4" in my vector. > > table() may not be the proper way to do it. > > For the moment, I use a loop which scans the vector from its lowest > value to its highest, and counts the number of times each value > appears. > > Is there a built-in function, or a table() option to do such a task > ? > > Thanks. > > Regards, > Nicolas > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
> Let's assume I have a vector of integers : > > myvector <- c(1, 2, 3, 2, 1, 3, 5) > For this, I create a table with ; > > mytable <- table(myvector) > 1 2 3 5 > 2 2 2 1 > However, table() returns an array of integers, mytable[4] returns the > occurence number of the "5" item, which makes this table hard to index. > I would prefer to have a data structure where mytable[4] could return 0, > as there is no "4" in my vector. >?tabulate so cumsum(tabulate(myvector))