Error using t.test() in R - not enough 'y'

2019-01-28 11:41发布

问题:

I got this error for my code

Error in t.test.default(dat$Value, x[[i]][[2]]) : 
  not enough 'y' observations

I think the reason I got this error is because I'm doing a t.test for data that only has one value (so there wouldnt be a mean or an sd) vs data that has 20 values..is there a way I can get around this.. is there a way I can ignore the data that doesn't have enough y observations??? like an if loop might work...pls help

So my code that does the t.test is

t<- lapply(1:length(x), function(i) t.test(dat$Value,x[[i]][[2]]))

where x is data in the form of cuts similar to

cut: [3:8)
        Number   Value
3       15        8
4       16        7
5       17        6
6       19        2.3
this data goes on 
cut:[9:14)
      Number   Value
7     21        15
cut:[13:18) etc
      Number    Value
8     22        49
9     23        15
10    24        13

How can I ignore 'cuts' that have only 1 value in them like the example above where in 'cut[9:14)' theres only one value....

回答1:

All standard variants of t-test use sample variances in their formulas, and you cannot compute that from one observation as you are dividing with n-1, where n is sample size.

This would probably be the easiest modification, although I cannot test it as you did not provide sample data (you could dput your data to your question):

 t<- lapply(1:length(x), function(i){
     if(length(x[[i]][[2]])>1){
       t.test(dat$Value,x[[i]][[2]]) 
     } else "Only one observation in subset" #or NA or something else
     })

Another option would be to modify the indices which are used in lapply:

ind<-which(sapply(x,function(i) length(i[[2]])>1))
t<- lapply(ind, function(i) t.test(dat$Value,x[[i]][[2]]))

Here's an example of the first case with artificial data:

x<-list(a=cbind(1:5,rnorm(5)),b=cbind(1,rnorm(1)),c=cbind(1:3,rnorm(3)))
y<-rnorm(20)

t<- lapply(1:length(x), function(i){
     if(length(x[[i]][,2])>1){ #note the indexing x[[i]][,2]
       t.test(y,x[[i]][,2]) 
     } else "Only one observation in subset"
     })

t
[[1]]

        Welch Two Sample t-test

data:  y and x[[i]][, 2] 
t = -0.4695, df = 16.019, p-value = 0.645
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -1.2143180  0.7739393 
sample estimates:
mean of x mean of y 
0.1863028 0.4064921 


[[2]]
[1] "Only one observation in subset"

[[3]]

        Welch Two Sample t-test

data:  y and x[[i]][, 2] 
t = -0.6213, df = 3.081, p-value = 0.5774
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -3.013287  2.016666 
sample estimates:
mean of x mean of y 
0.1863028 0.6846135 


        Welch Two Sample t-test

data:  y and x[[i]][, 2] 
t = 5.2969, df = 10.261, p-value = 0.0003202
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 3.068071 7.496963 
sample estimates:
mean of x mean of y 
5.5000000 0.2174829 


标签: r cut