在R中使用t.test()时出错-不足';y';观察

在R中使用t.test()时出错-不足';y';观察,r,cut,R,Cut,我的代码出现了这个错误 Error in t.test.default(dat$Value, x[[i]][[2]]) : not enough 'y' observations 我想我产生这个错误的原因是因为我对只有一个值的数据(所以不会有平均值或标准差)和有20个值的数据进行了t检验。有没有办法解决这个问题。。有没有一种方法可以忽略没有足够的y观测值的数据???像一个if循环可能会工作…请帮助 所以我做t检验的代码是 t<- lapply(1:length(x), functi

我的代码出现了这个错误

Error in t.test.default(dat$Value, x[[i]][[2]]) : 
  not enough 'y' observations
我想我产生这个错误的原因是因为我对只有一个值的数据(所以不会有平均值或标准差)和有20个值的数据进行了t检验。有没有办法解决这个问题。。有没有一种方法可以忽略没有足够的y观测值的数据???像一个if循环可能会工作…请帮助

所以我做t检验的代码是

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

我怎么能忽略其中只有一个值的“切割”,就像上面的例子,在“切割[9:14]”中只有一个值……

t-test的所有标准变量都在公式中使用样本方差,并且当你用n-1除以n是样本大小时,你不能从一个观察值中计算出来

这可能是最简单的修改,尽管我无法测试它,因为您没有提供示例数据(您可以
dput
您的问题的数据):

以下是第一个人工数据案例的示例:

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 

xI编辑了我的问题并添加了一些数据…..我尝试过你给我的,但我得到了错误,如错误:意外的“{”在“t1”{”Oops中,代码缺少一个括号,我更正了代码,现在应该可以工作了。
 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
     })
ind<-which(sapply(x,function(i) length(i[[2]])>1))
t<- lapply(ind, function(i) t.test(dat$Value,x[[i]][[2]]))
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