t.test()的使用中~和,的区别
> y <-1:10
> x <- rep(letters[1:2],5)
> t.test(y~x)
Welch Two Sample t-test
data: y by a
t = -0.5, df = 8, p-value = 0.6305
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-5.612008 3.612008
sample estimates:
mean in group a mean in group b
5 6
~是讲y和x关联起来了,x="a"的时候y可以为[1,3,5,7,9],所以"a"的均值为5,为“b”的可以为[2,4,6,8,10]。这时候用‘,’就会报错,因为x不是数,不能直接得到统计量。
> y <-1:10
> x <- rep(letters[1:2],5)
> t.test(y,x)
Error in if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) stop("data are essentially constant") :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In mean.default(y) : argument is not numeric or logical: returning NA
2: In var(y) : NAs introduced by coercion
对于x,y为分别的两组数的时候,判断他们之间分布是不是一致,就不能用~。使用~这种关联结构,t.test只能判断x只能为两个值的情况。
> x <- c(rnorm(10))
> y<- c(rnorm(10))
> t.test(x~y)
Error in t.test.formula(x ~ y) :
grouping factor must have exactly 2 levels
这种情况应该用","。
> x <- c(rnorm(10))
> y<- c(rnorm(10))
> t.test(y,x)
Welch Two Sample t-test
data: y and x
t = -0.053873, df = 17.045, p-value = 0.9577
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.9223157 0.8763781
sample estimates:
mean of x mean of y
-0.2160393 -0.1930705
在plot()的时候:
plot(y~x)
plot(y,x)
前者,~左边为因变量,后者“,”后面为因变量。