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"). In addition, p-value results of each t-test should be printed into a new data frame.
I think there needs to be an array/do-loop set up, but even coding for the t-test has been excruciating. 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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.