Nick - 10 months ago 53

R Question

I'm an R novice, and I can't seem to get this one: I want to aggregate factors in a dataframe by counting occurances of a particular value (in this case, of the value "Yes"). There are lots of N/As as well, as follows:

Current:

`Agg Factor1 Factor2 Factor3`

A1 Yes Yes Yes

A1 Yes Yes No

A1 Yes N/A No

A1 N/A No N/A

A2 Yes Yes No

A2 N/A Yes N/A

A2 No N/A N/A

Desired result:

`Agg F1 F2 F3`

A1 3 2 1

A2 1 2 0

Ideally I'd like an answer is base R, as I'm trying to learn.. of course, if there's an easier solution that uses a package, I'd also be interested.

Data

`dd <- read.table(header = TRUE, na.strings = "N/A", text =`

"Agg Factor1 Factor2 Factor3

A1 Yes Yes Yes

A1 Yes Yes No

A1 Yes N/A No

A1 N/A No N/A

A2 Yes Yes No

A2 No Yes N/A

A2 N/A No N/A")

Answer Source

Comparisons against a factor work against the labels, so this will work regardless whether the Factor columns are factors or strings:

```
aggregate(dd[-1L]=='Yes',dd[1L],sum,na.rm=T);
## Agg Factor1 Factor2 Factor3
## 1 A1 3 2 1
## 2 A2 1 2 0
```