R:在数据帧或矩阵中使用t.test函数 [英] R : use of t.test function in data frame or matrix
问题描述
我想使用t.test函数来比较存储在数据帧中的值组。假设我的数据框有2列:组和结果以及40行。 结果列包含我想要比较的值,组列表示其中值被划分的组:例如4组(a,b,c,d),每组10个值。
I would like to use the t.test function to compare groups of values stored in a dataframe. Let say my dataframe has 2 columns : "group" and "result" and 40 lines. The "result" column contains the values I want to compare, and the "group" column indicates the groups in which the values are divided : for example 4 groups (a,b,c,d) of 10 values each.
如何表示我只想测试属于group a的值与属于b组的值?
How can I indicate that I only want to test the values belonging to group a versus the values belonging to group b ?
或者,是否有一种简单的方法来将属于组a的值提取为向量(让我们称之为vecta),以便随意比较向量?
Alternatively, is there a simple way to extract the values belonging to the group a into a vector (let's call it "vecta") in order to compare the vectors at will ?
提前谢谢!
Seb
Thanks in advance ! Seb
推荐答案
您问:如何表示我只想测试属于组a的值相对于属于组b的值?
You asked: "How can I indicate that I only want to test the values belonging to group a versus the values belonging to group b ?"
假设您的数据框称为 df
。要将a组与b组与t.test进行比较,您可以使用例如:
Suppose your data frame is called df
. To compare group a with group b with t.test you can use e.g:
t.test(df$result[df$group=="a"], df$result[df$group=="b"])
# or
with(df, t.test(result[group=="a"], result[group=="b"]))
# or, fo rexample
t.test(result~group, data=subset(df, group %in% c("a", "b")))
所有方法都应该工作,但未经测试,因为您没有发布任何示例数据: P
All approaches should work but are untested as you didn't post any example data:P
或者,是否有一种简单的方法来将属于组a的值提取为向量(让我们称之为vecta),以便比较向量有意愿吗?
"Alternatively, is there a simple way to extract the values belonging to the group a into a vector (let's call it "vecta") in order to compare the vectors at will ?"
是,
df$value[def$group=="a"] # result is a vector
这篇关于R:在数据帧或矩阵中使用t.test函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!