rgroupingfactors

Basic - T-Test -> Grouping Factor Must have Exactly 2 Levels


I am relatively new to R. For my assignment I have to start by conducting a T-Test by looking at the effect of a politician's (Conservative or Labour) wealth on their real gross wealth and real net wealth. I have to attempt to estimate the effect of serving in office wealth using a simple t-test.

The dataset is called takehome.dta

Labour and Tory are binary where 1 indicates that they serve for that party and 0 otherwise.

The variables for wealth are lnrealgross and lnrealnet.

I have imported and attached the dataset, but when I attempt to conduct a simple t-test. I get the following message "grouping factor must have exactly 2 levels." Not quite sure where I appear to be going wrong. Any assistance would be appreciated!


Solution

  • are you doing this:

    t.test(y~x)
    

    when you mean to do this

    t.test(y,x)
    

    In general use the ~ then you have data like

    y <- 1:10
    x <- rep(letters[1:2], each = 5)
    

    and the , when you have data like

    y <- 1:5
    x <- 6:10
    

    I assume you're doing something like:

    y <- 1:10
    x <- rep(1,10)
    t.test(y~x) #instead of t.test(y,x)
    

    because the error suggests you have no variation in the grouping factor x