Unpaired T-test not recognising "object"

I want to do an unpaired T-test, but I don't know how I plug my data into the command. It just says that the columns from my data aren't recognized.

Plot Shaded Exposed
1 1 64 321
2 2 16 147
3 3 47 380
4 4 10 415
5 5 22 130
6 6 54 271

whats the name of your dataframe that contains the variables Plot, Shaded and Exposed ? you would need to provide it to t.test with the data= param

How do I do that? Could you provide an example please.

You seem focused on my second sentence, but not my first...

I've no idea what you are talking about. I'm asking for an example of how to do the "data=param" thing you talked about. I don't know what that is.

Type out your code here and I'll add the data Param to it with a made up dataframe name that you can then correct from your own. But I don't feel like typing the content of your image.

my_data <- read.csv("Shade_Exposed.csv")
summary.data.frame(my_data)
t.test(Shaded~Exposed, mu = 0, alt ="two.sided", conf = 0.95, var.eq = F,
paired = F)

my_data <- read.csv("Shade_Exposed.csv")
summary.data.frame(my_data)
t.test(data=my_data , Shaded~Exposed, mu = 0, alt ="two.sided", conf = 0.95, var.eq = F,
paired = F)

It is giving me an error saying " grouping factor must contains exactly two levels".

I've managed to fix that but it is still saying "shaded not found"

what does
names(my_data)
show ?

[1] "Plot" "Shaded" "Exposed"

does it say shaded not found, or Shaded not found ?

Error in t.test(data = my_data, Shaded, Exposed, mu = 0, alt = "two.sided", :
object 'Shaded' not found

looks like you stopped using the formula style syntax ? (featuring the tilde symbol ~)

BEcause if I do that I get another issues saying there must be exactly two levels.

There are two ways to do it.
To use the formula approach requires restructuring your data so as to have a grouping variable


my_data<- tibble::tribble(
  ~Plot,~Shaded,~Exposed,
          1, 64, 321,
          2, 16, 147,
          3, 47, 380,
          4, 10, 415,
          5, 22, 130,
          6, 54, 271
  )
ttest_a <- t.test(x=my_data$Exposed , y=my_data$Shaded,
       mu = 0,
       alternative ="two.sided",
       conf = 0.95, 
       var.eq = F,
       paired = F) 

# for formula need different data structure
my_data2 <- pivot_longer(my_data,
                         cols=c(Shaded,Exposed),
                         names_to = "groupname") 

ttest_b <- t.test(value~groupname,data=my_data2)

#compare the results
ttest_a
ttest_b

The top one has worked for me, no errors. But I'm not getting a result.

the bottom one relies on library(tidyverse) to pivot_longer the data. sorry I didnt say.

you need to run ttest_a to see whats in test_a...

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.