Hi, I am sure there is an easy way to do it, but I can't find it. I have a data frame that has 15 columns and 7000 rows. The only values inside the data.frame are "aa", "ab", "bb" as you can see an example bellow. 1 2 3 1 aa ab ab 2 ab ab ab 3 aa aa aa 4 bb bb bb What I would like to do, is to generate a vector (or another data.frame) with 7000 rows, and 3 columns. In the first column, the information about how many aa, the second about how many ab, and the third about how many bb are there in each line aa ab bb 1 1 2 0 2 0 2 0 3 3 0 0 4 0 0 3 Thank you very much Cheers -- View this message in context: http://r.789695.n4.nabble.com/Counting-Frequencies-in-Data-Frame-tp2221342p2221342.html Sent from the R help mailing list archive at Nabble.com.
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of M.Ribeiro > Sent: Tuesday, May 18, 2010 7:13 AM > To: r-help at r-project.org > Subject: [R] Counting Frequencies in Data Frame > > > Hi, > I am sure there is an easy way to do it, but I can't find it. > I have a data frame that has 15 columns and 7000 rows. > > The only values inside the data.frame are "aa", "ab", "bb" as > you can see an > example bellow. > > 1 2 3 > 1 aa ab ab > 2 ab ab ab > 3 aa aa aa > 4 bb bb bb > > What I would like to do, is to generate a vector (or another > data.frame) > with 7000 rows, and 3 columns. In the first column, the > information about > how many aa, the second about how many ab, and the third > about how many bb > are there in each line > aa ab bb > 1 1 2 0 > 2 0 2 0--> 3? <--> 3 3 0 0 > 4 0 0 3You could make a "table" (a sort of "matrix") of the data entries and their row numbers: > tmp <- read.table(header=TRUE, check.names=FALSE, textConnection(" + 1 2 3 + 1 aa ab ab + 2 ab ab ab + 3 aa aa aa + 4 bb bb bb + ")) > table(row(tmp), as.matrix(tmp)) aa ab bb 1 1 2 0 2 0 3 0 3 3 0 0 4 0 0 3 The as.matrix() is needed to force all the columns of the data.frame into one vector. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > Thank you very much > Cheers > -- > View this message in context: > http://r.789695.n4.nabble.com/Counting-Frequencies-in-Data-Frame-tp2221342p2221342.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Hi, Others will have fancier solutions, but is the way I would do it: dat <- read.table(textConnection("1 2 3 1 aa ab ab 2 ab ab ab 3 aa aa aa 4 bb bb bb"), header=TRUE) closeAllConnections() countAB <- function(x) { aa <- length(which(x == "aa")) ab <- length(which(x == "ab")) bb <- length(which(x == "bb")) Result <- c(aa, ab, bb) return(Result) } Counts <- as.data.frame(t(apply(dat, 1, countAB))) names(Counts) <- c("aa", "ab", "bb") Best, Ista On Tuesday 18 May 2010 10:12:49 am M.Ribeiro wrote:> Hi, > I am sure there is an easy way to do it, but I can't find it. > I have a data frame that has 15 columns and 7000 rows. > > The only values inside the data.frame are "aa", "ab", "bb" as you can see > an example bellow. > > 1 2 3 > 1 aa ab ab > 2 ab ab ab > 3 aa aa aa > 4 bb bb bb > > What I would like to do, is to generate a vector (or another data.frame) > with 7000 rows, and 3 columns. In the first column, the information about > how many aa, the second about how many ab, and the third about how many bb > are there in each line > aa ab bb > 1 1 2 0 > 2 0 2 0 > 3 3 0 0 > 4 0 0 3 > > Thank you very much > Cheers
Possibly Parallel Threads
- String frequencies in rows
- rbinding some elements from a list and obtain another list
- how to convert a data.frame to tree structure object such as dendrogram
- Counting occurences of variables in a dataframe
- Help needed for efficient way to loop through rows and columns