Hi Laura,
Usually this kind of thing is easier if you put your data into a "long"
format. I would use something like

Dat <- read.table(textConnection("subject A  B C D E
1 cat monkey cat dog cat
2 cat cat cat cat dog"), header=TRUE)

library(reshape)
m.Dat <- melt(Dat, id="subject")
xtabs(~subject+value, m.Dat)
       value
subject cat monkey dog
      1   3      1   1
      2   4      0   1

but you could use the reshape function instead of reshape::melt.

-Ista

On Fri, Apr 16, 2010 at 7:18 AM, Laura Ferrero-Miliani <laurafe@gmail.com>wrote:

> Hi everyone!
> I am very new to R and I am having some difficulties.
> My data set looks something like this:
>
> subject                A                B                C
>    D               E
>       1                  cat             monkey       cat
> dog            cat
>       2                  cat             cat               cat
>     cat             dog
> ....
>
> I want to create three new variables, that count the amount of "cat",
> "monkey" and "dog" per subject
>
> subject                A                B                C
>   D               E            cat             dog             monkey
>       1                  cat             monkey       cat
> dog            cat          3               1                 1
>       2                  cat             cat               cat
>     cat             dog        4                1                 0
> ....
>
> I have been looking at rowSums, rowsum, apply, grep, and doing some
> searches, but I can only find count for numerical values or NA values.
>
> Thanks in advance,
>
> L
>
> ______________________________________________
> 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

	[[alternative HTML version deleted]]

