Search code examples
rhypothesis-test

Running T.Test in R comparing two different columns


I have to run a t.test in R on the following: If the YearsAtCompany of Single is less than Married

Meaning somehow I need to compare the YearsatCompany with Marital Status

I have tried:

t.test(EmployeeAttrition$YearsAtCompany[Single],EmployeeAttrition$YearsAtCompany[Married],alternative = "less")

But that isn't giving me the information correctly. I am fairly new at R. And have researched other questions on here and still can't get the code right.

Sample data:

Data Screenshot


Solution

  • You can use with and properly subset your data (You needed to specific the variable in EmployeeAttrition to subset by).

    with(EmployeeAttrition, t.test(YearsAtCompany[MaritalStatus == "Single"], YearsAtCompany[MaritalStatus == "Married"], alternative = "less")