0

Sample data

df <- data.frame("id"=c(1,2,3,4,5), "group"=c(0,0,1,1,1), "score"=c(10,14,15,13,12))

My goal is to compare the scores for group = 1 to the complete sample.

I figured how to do the t test:

t.test(df$score ~ df$group)

But is this for group = 1 versus group = 0?

bvowe
  • 3,004
  • 3
  • 16
  • 33

2 Answers2

0

You can subset your df to only keep one group and then in the original data make all the groups the same. Combine the two data.frames so now you have the complete sample as one group and group one still separate. Then do the t-test.

library(dplyr)
df <- data.frame("id"=c(1,2,3,4,5), 
                 "group"=c(0,0,1,1,1), 
                 "score"=c(10,14,15,13,12))

#make group = 2 so this is the 'complete sample'
df2 <- mutate(df, group = 2)
#keep only group 1
df1 <- filter(df, group == 1)
#put together so that you have group 1 vs complete sample
df3 <- rbind(df1,df2)

#do t-test
t.test(df3$score,df3$group)
Mike
  • 3,797
  • 1
  • 11
  • 30
0

Alternately, you could just

t.test(df$score[df$group==1], df$score)
Matt Tyers
  • 2,125
  • 1
  • 14
  • 23