-1

I have a dataset (df) where I would just like to get some summary stats for the entire column variables and then a summary for the variables of 2 specific treatments. So far so good:

summary(var1)
aggregate(var1 ~ treatment, results, summary)

I then have one variable that are values of 1 and 2. I can count these with the sum function:

sum(var3 == 1)
sum(var3 == 2)

However, when I try to sum these by treatment:

aggregate(var3 ~ treatment, results, sum var3 == 1)

I get the following error:

Error in sum == 1 : 
comparison (1) is possible only for atomic and list types

I have tried lots of variations on the same theme and taken a look through the textbooks I am using to help me with my first forays into R... but I can't seem to find the answer.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
J Freeman
  • 3
  • 2

1 Answers1

2

Here's a sample dataset (it's always best to include sample data to make your question reproducible).

set.seed(15)
results<-data.frame(
    var1=runif(30), 
    var3=sample(1:2, 30, replace=T), 
    treatment=gl(2,15)
)

If you really want to use aggregate, you can do

aggregate(var3==1~treatment, results, sum)
#   treatment var3 == 1
# 1         1         9
# 2         2         5

but since you're counting discrete observations, table() may be a better choice to do all the counting at once

with(results, table(var3, treatment))
#     treatment
# var3  1  2
#    1  9  5
#    2  6 10
Community
  • 1
  • 1
MrFlick
  • 195,160
  • 17
  • 277
  • 295