简体   繁体   English

R中的多个成对T检验(按类别分层)显示了数据集的变量和p值部分

[英]Multiple Paired T-Tests in R stratified by category showing variable and p-values part of the dataset

Coders, 编码器,

I have a dataset that records types of cars in each country before and after the global recession. 我有一个数据集,记录了全球经济衰退前后每个国家/地区的汽车类型。

I want to do a paired t-test comparing the number of cars of all countries ( Country ) before ( Before ) and after( After ) for each car type ("cartype"). 我想做一个配对的t检验,比较每种汽车类型(“ cartype”)之前( Before )和之后( After )的所有国家( Country )的汽车数量。 In addition, p-value results of each t-test should be printed into a new data frame. 此外,每个t检验的p值结果应打印到新的数据框中。

I think there needs to be an array/do-loop set up, but even coding for the t-test has been excruciating. 我认为需要建立一个数组/ do-loop,但即使是t检验的编码也很麻烦。 Help! 救命!

Thanks! 谢谢!

##insert data
example<- read.table (header=TRUE, text=" Country Cartype before after  
UAE LHR 17  91
UAE AUH 50  30
UAE DXB 72  85
UAE DOH 19  8
UAE AMS 72  98
UAE FRA 6   3
UAE CDG 14  39
UAE BOM 81  65
UAE DEL 31  55
UAE ABV 85  50
IN  LHR 42  100
IN  AUH 6   96
IN  DXB 36  82
IN  DOH 15  20
IN  AMS 33  76
IN  FRA 17  1
IN  CDG 71  52
IN  BOM 51  84
IN  DEL 29  25
IN  ABV 74  71
PK  LHR 35  15
PK  AUH 27  83
PK  DXB 67  8
PK  DOH 98  51
PK  AMS 44  16
PK  FRA 41  14
PK  CDG 80  52
PK  BOM 76  74
PK  DEL 42  91
PK  ABV 50  95
")


## calculate how many cartypes in total
n=length(table(example$Cartype)) 

## create a list with empty object to store the t test result later
result=vector(mode = list, length = n) 

## use do loop to perform t-test by cartype
for (i in 1:n) 
{
    y1=example$before[which(example$cartype==i)]
    y2=example$after[which(example$cartype==i)]
    result[[i]]=t.test(y1,y2, paired=T)
}
### print the result
result 

Thanks for the effort of formatting 感谢格式化的努力

First, be aware that R is case sensitive, so cartype is different of Cartype 首先,请注意R是区分大小写的,因此cartypeCartype不同

I think this code is what you were trying to put together: 我认为这段代码就是您想要组合的代码:

n=length(table(example$Cartype)) 
result=vector(length = n) 
cartype <- unique(example$Cartype)
for (i in 1:n) 
{
        y1=example[example$Cartype==cartype[[i]],'before']
        y2=example[example$Cartype==cartype[[i]],'after']
        result[[i]]=t.test(y1,y2, paired=T)$p.value
}
df <- data.frame(row.names = cartype,p.value=result)

      p.value
LHR 0.3273000
AUH 0.3256148
DXB 1.0000000
DOH 0.3694496
AMS 0.5884953
FRA 0.1576278
CDG 0.6980914
BOM 0.7642104
DEL 0.2718261
ABV 0.9292062

You can make it shorter as: 您可以将其缩短为:

p.values=c(
        by(example,
           example$Cartype,
           FUN=function(x) t.test(x$before,x$after, paired=T)$p.value))
df <- data.frame(p.values)

     p.values
ABV 0.9292062
AMS 0.5884953
AUH 0.3256148
BOM 0.7642104
CDG 0.6980914
DEL 0.2718261
DOH 0.3694496
DXB 1.0000000
FRA 0.1576278
LHR 0.3273000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM