I have a dataframe df with two columns x and y. I want to count the number of times a unique x, y combination occurs. For example x<- c(1,2,3,4,5,1,2,3,4) y<- c(1,2,3,4,5,1,2,4,1) df<-as.data.frame(cbind(x, y)) #what is the correct way to use ddply for this example? ddply(df, c('x','y', summarize, ??) #desired output -- format and order doesn't matter # (x, y) count #-------------------- # (1, 1) 2 # (2, 2) 2 # (3, 3) 1 # (4, 4) 1 # (5, 5) 1 # (2, 3) 1 # (3, 4) 1 # (4, 1) 1 [[alternative HTML version deleted]]
Hi Idris, I do not know what the correct use of ddply would be, but here is another option using paste() and table(); data.frame(with(df, table(paste("(", x, ",", y, ")", sep = "")))) HTH, Jorge On Tue, Jun 21, 2011 at 2:30 PM, Idris Raja <> wrote:> I have a dataframe df with two columns x and y. I want to count the number > of times a unique x, y combination occurs. > > For example > > x<- c(1,2,3,4,5,1,2,3,4) > y<- c(1,2,3,4,5,1,2,4,1) > > df<-as.data.frame(cbind(x, y)) > > #what is the correct way to use ddply for this example? > ddply(df, c('x','y', summarize, ??) > > #desired output -- format and order doesn't matter > # (x, y) count > #-------------------- > # (1, 1) 2 > # (2, 2) 2 > # (3, 3) 1 > # (4, 4) 1 > # (5, 5) 1 > # (2, 3) 1 > # (3, 4) 1 > # (4, 1) 1 > > [[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]]
Hi: count() is a much faster function than ddply() in this circumstance. x<- c(1,2,3,4,5,1,2,3,4) y<- c(1,2,3,4,5,1,2,4,1) d <- data.frame(x, y) library(plyr) count(d, c('x', 'y')) x y freq 1 1 1 2 2 2 2 2 3 3 3 1 4 3 4 1 5 4 1 1 6 4 4 1 7 5 5 1 HTH, Dennis On Tue, Jun 21, 2011 at 11:30 AM, Idris Raja <idris.raja at gmail.com> wrote:> I have a dataframe df with two columns x and y. I want to count the number > of times a unique x, y combination occurs. > > For example > > x<- c(1,2,3,4,5,1,2,3,4) > y<- c(1,2,3,4,5,1,2,4,1) > > df<-as.data.frame(cbind(x, y)) > > #what is the correct way to use ddply for this example? > ddply(df, c('x','y', summarize, ??) > > #desired output -- format and order doesn't matter > # (x, y) count > #-------------------- > # (1, 1) 2 > # (2, 2) 2 > # (3, 3) 1 > # (4, 4) 1 > # (5, 5) 1 > # (2, 3) 1 > # (3, 4) 1 > # (4, 1) 1 > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
On 6/21/2011 11:30 AM, Idris Raja wrote:> I have a dataframe df with two columns x and y. I want to count the number > of times a unique x, y combination occurs. > > For example > > x<- c(1,2,3,4,5,1,2,3,4) > y<- c(1,2,3,4,5,1,2,4,1) > > df<-as.data.frame(cbind(x, y)) > > #what is the correct way to use ddply for this example? > ddply(df, c('x','y', summarize, ??) > > #desired output -- format and order doesn't matter > # (x, y) count > #-------------------- > # (1, 1) 2 > # (2, 2) 2 > # (3, 3) 1 > # (4, 4) 1 > # (5, 5) 1 > # (2, 3) 1 > # (3, 4) 1 > # (4, 1) 1 > > [[alternative HTML version deleted]]Jorge and Dennis gave good responses that get you to the result you asked for, but for completeness I thought I'd include some ddply versions: ddply(d, .(x, y), summarize, freq=length(x)) This uses the summarize function you were asking about, however you can also do it with: ddply(d, .(x, y), nrow) or ddply(d, .(x, y), as.data.frame(nrow)) The latter giving a slightly nicer name (value instead of V1). As an aside, I prefer using the "summarise" spelling of the function when I do use it, because it won't clash with Hmisc::summarize. ddply(d, .(x, y), summarise, freq=length(x)) -- Brian S. Diggs, PhD Senior Research Associate, Department of Surgery Oregon Health & Science University
On Jun 21, 2011, at 2:30 PM, Idris Raja wrote:> I have a dataframe df with two columns x and y. I want to count the > number > of times a unique x, y combination occurs. > > For example > > x<- c(1,2,3,4,5,1,2,3,4) > y<- c(1,2,3,4,5,1,2,4,1) > > df<-as.data.frame(cbind(x, y)) > > #what is the correct way to use ddply for this example? > ddply(df, c('x','y', summarize, ??) > > #desired output -- format and order doesn't matter > # (x, y) count > #-------------------- > # (1, 1) 2 > # (2, 2) 2 > # (3, 3) 1 > # (4, 4) 1 > # (5, 5) 1 > # (2, 3) 1 > # (3, 4) 1 > # (4, 1) 1 >Here's a non plyr approach: > tab.df <- as.data.frame(table(df)) > tab.df[tab.df$Freq > 0, ] x y Freq 1 1 1 2 4 4 1 1 7 2 2 2 13 3 3 1 18 3 4 1 19 4 4 1 25 5 5 1 Did you know that there is a newsgroup specifically set up for plyr questions? manipulatr at googlegroups.com -- David Winsemius, MD West Hartford, CT