Im trying to do a t test between 2 variables, price of a building and the size of a building.
Size consists of either "large" or "small" and Price can be any number (54,2123,325...)
i did this:
data$size <- as.factor(data$size)
t.test(data$size ~ data$price, data = data)
But i get this error and I cant solve it:
Error in t.test.formula(data$size ~ data$price, :
grouping factor must have exactly 2 levels
I am a beginner in R and would appreciate any help here you can see the data
CodePudding user response:
You must reverse the order of the variables that you put in t.test, first comes the numeric variable (price) and then the binary variable (size).
size <- c("large","large","small","small","small","small")
price <- c(3536,2239,1982,1769,1769,1769)
data <- data.frame(size, price)
data$size <- as.factor(data$size)
t.test(data$price ~ data$size, data = data)
CodePudding user response:
I think you need this:
with(data, t.test(price[size == "large"], price[size=="small"]))
CodePudding user response:
First, subset the data into small and large:
small <- subset(data, size == "small",price, drop = T)
large <- subset(data, size != "small",price, drop = T)
Next ensure that your data pass a test of homoscedasticity--are the variances homogenous? We can use var.test()
for that:
var.test(small, large)
Finally, depending on whether the p-value came out >0.05 (var.equal = TRUE
) or <0.05 (var.equal = FALSE)
we perform a t.test()
:
t.test(small, large, var.equal = TRUE)