[R] count number of groups
Hello, Simple question that I am stuck on and can't seem to find an answer in the help files currently. I have a list which contains repeated ID's. I would like to have R count the number of ID's. For example: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) as.data.frame(ID) Clearly, there are 3 groups. How would I have R give me the summary: ID 3 Many thanks, Charles [[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.
Re: [R] count number of groups
length(unique(ID)) Michael On Fri, May 25, 2012 at 11:38 AM, Charles Determan Jr deter...@umn.edu wrote: Hello, Simple question that I am stuck on and can't seem to find an answer in the help files currently. I have a list which contains repeated ID's. I would like to have R count the number of ID's. For example: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) as.data.frame(ID) Clearly, there are 3 groups. How would I have R give me the summary: ID 3 Many thanks, Charles [[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. __ 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.
Re: [R] count number of groups
Hi. try using table function: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) table(IF) ID 1 2 3 4 4 4 Also check ?tapply function Andrija On Fri, May 25, 2012 at 5:38 PM, Charles Determan Jr deter...@umn.eduwrote: Hello, Simple question that I am stuck on and can't seem to find an answer in the help files currently. I have a list which contains repeated ID's. I would like to have R count the number of ID's. For example: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) as.data.frame(ID) Clearly, there are 3 groups. How would I have R give me the summary: ID 3 Many thanks, Charles [[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. [[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.
Re: [R] count number of groups
Thank you Michael, However, this only provides the number of groups without a column label. Is there a way to have it give the count with the 'ID' label? Regards, Charles On Fri, May 25, 2012 at 10:52 AM, R. Michael Weylandt michael.weyla...@gmail.com wrote: length(unique(ID)) Michael On Fri, May 25, 2012 at 11:38 AM, Charles Determan Jr deter...@umn.edu wrote: Hello, Simple question that I am stuck on and can't seem to find an answer in the help files currently. I have a list which contains repeated ID's. I would like to have R count the number of ID's. For example: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) as.data.frame(ID) Clearly, there are 3 groups. How would I have R give me the summary: ID 3 Many thanks, Charles [[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. [[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.
Re: [R] count number of groups
That works if I want a count of each group but I needed a count of the number of groups. Michael answered that question with length(unique(ID)) However, this doesn't supply a title, it is just a number. I need it to still have the identifier 'ID'. Regards, Charles On Fri, May 25, 2012 at 11:50 AM, Steve Friedman skfgla...@gmail.comwrote: ?table On May 25, 2012 11:46 AM, Charles Determan Jr deter...@umn.edu wrote: Hello, Simple question that I am stuck on and can't seem to find an answer in the help files currently. I have a list which contains repeated ID's. I would like to have R count the number of ID's. For example: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) as.data.frame(ID) Clearly, there are 3 groups. How would I have R give me the summary: ID 3 Many thanks, Charles [[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. [[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.
Re: [R] count number of groups
You'll have to be a little trickier if you want it to be smart and pick up the name: [or I'm missing something obvious] yourFunc - function(x){ dsx - deparse(substitute(x)) x - length(unique(x)) names(x) - dsx x } yourFunc(ID) yourFunc(ID^2) yourFunc(ID[ID==2]) etc. Hope this helps, M On Fri, May 25, 2012 at 12:42 PM, Charles Determan Jr deter...@umn.edu wrote: Thank you Michael, However, this only provides the number of groups without a column label. Is there a way to have it give the count with the 'ID' label? Regards, Charles On Fri, May 25, 2012 at 10:52 AM, R. Michael Weylandt michael.weyla...@gmail.com wrote: length(unique(ID)) Michael On Fri, May 25, 2012 at 11:38 AM, Charles Determan Jr deter...@umn.edu wrote: Hello, Simple question that I am stuck on and can't seem to find an answer in the help files currently. I have a list which contains repeated ID's. I would like to have R count the number of ID's. For example: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) as.data.frame(ID) Clearly, there are 3 groups. How would I have R give me the summary: ID 3 Many thanks, Charles [[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. __ 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.
Re: [R] count number of groups
You did: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) as.data.frame(ID) and I infer that you want the number of groups in each column of the data.frame. First, make an example of your data.frame D - data.frame(ID, Name=rep(state.name[1:7],len=length(ID))) (note I use data.frame, not as.data.frame, so it gets the column names that I want). Now use sapply() (or the related vapply()): sapply(D, function(column)length(unique(column))) ID Name 37 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Charles Determan Jr Sent: Friday, May 25, 2012 9:55 AM To: Steve Friedman Cc: r-help@r-project.org Subject: Re: [R] count number of groups That works if I want a count of each group but I needed a count of the number of groups. Michael answered that question with length(unique(ID)) However, this doesn't supply a title, it is just a number. I need it to still have the identifier 'ID'. Regards, Charles On Fri, May 25, 2012 at 11:50 AM, Steve Friedman skfgla...@gmail.comwrote: ?table On May 25, 2012 11:46 AM, Charles Determan Jr deter...@umn.edu wrote: Hello, Simple question that I am stuck on and can't seem to find an answer in the help files currently. I have a list which contains repeated ID's. I would like to have R count the number of ID's. For example: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) as.data.frame(ID) Clearly, there are 3 groups. How would I have R give me the summary: ID 3 Many thanks, Charles [[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. [[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. __ 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.
Re: [R] count number of groups
That works perfectly, thank you very much Michael, Sincere regards, Charles On Fri, May 25, 2012 at 11:59 AM, R. Michael Weylandt michael.weyla...@gmail.com wrote: You'll have to be a little trickier if you want it to be smart and pick up the name: [or I'm missing something obvious] yourFunc - function(x){ dsx - deparse(substitute(x)) x - length(unique(x)) names(x) - dsx x } yourFunc(ID) yourFunc(ID^2) yourFunc(ID[ID==2]) etc. Hope this helps, M On Fri, May 25, 2012 at 12:42 PM, Charles Determan Jr deter...@umn.edu wrote: Thank you Michael, However, this only provides the number of groups without a column label. Is there a way to have it give the count with the 'ID' label? Regards, Charles On Fri, May 25, 2012 at 10:52 AM, R. Michael Weylandt michael.weyla...@gmail.com wrote: length(unique(ID)) Michael On Fri, May 25, 2012 at 11:38 AM, Charles Determan Jr deter...@umn.edu wrote: Hello, Simple question that I am stuck on and can't seem to find an answer in the help files currently. I have a list which contains repeated ID's. I would like to have R count the number of ID's. For example: ID=c(1,1,1,1,2,2,2,2,3,3,3,3) as.data.frame(ID) Clearly, there are 3 groups. How would I have R give me the summary: ID 3 Many thanks, Charles [[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. [[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.