I am a newbie in R and currently I'm crash into this problem when trying to do a t-test.
I am working on the data set Cars93 in MASS, filter out type t1 (Compact) and type t2 (Large) from Type in Cars93 to get 2 separate dataframe called df.t1 and df.t2
Now I want to compare the means of MPG.City of type t1 and type t1
What I have done are
var.test(df.t1$MPG.city ~ df.t2$MPG.city)
t.test(Cars93$MPG.city[Cars93$Type ==`type t1`]~ Cars93$MPG.city[Cars93$Type ==`type t2`])
However R shows:
Error in model.frame.default(formula = df.t1$MPG.city ~ df.t2$MPG.city):
variable lengths differ (found for 'df.t2$MPG.city')
Please if anyone could help me look into this. I would be truly appreciated
CodePudding user response:
You dont need to subset the data in the way you have described (creating two separate data.frames, which result in different lengths and throw an error) - try this:
data("Cars93", package = "MASS")
df <- Cars93[Cars93$Type %in% c("Compact", "Large"), ]
var.test(df$MPG.city ~ df$Type)
t.test(df$MPG.city ~ df$Type)