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 ______________________________________________ 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.