问题
The common format of a data frame for peforming a Paired t-test in R is a given measurement divided in two columns, so that each row indicates a same subject or individual. For example:
> # Before After
> #1 31 32
> #2 22 34
> #3 41 35
However, this is not always the case in which the data are presented in a data frame.
I have a data frame which structure is very common and looks like this:
subject <- c("A1", "A2" ,"A1" ,"A3" ,"A3" ,"A2")
value <- c(34, 43, 25, 43, 54, 22)
group <- c("before", "after", "after", "after", "before", "before")
mydata <- data.frame(subject, value, group)
# subject value group
#1 A1 34 before
#2 A2 43 after
#3 A1 25 after
#4 A3 43 after
#5 A3 54 before
#6 A2 22 before
So, based on this data frame how can I perform a two-sided paired t-test in R?
回答1:
What about some R base function to have data from long to wide format:
# reshape from long to wide
mydata_wide <- reshape(mydata, idvar = "subject", timevar = "group", direction = "wide")
# rename columns
colnames(mydata_wide)[2] <-"before"
colnames(mydata_wide)[3] <-"after"
mydata_wide
subject before after
1 A1 34 25
2 A2 22 43
4 A3 54 43
# t-test
t.test(mydata_wide$before,
mydata_wide$after,
paired=TRUE,
conf.level=0.95)
来源:https://stackoverflow.com/questions/52461376/how-to-perform-a-paired-t-test-in-r-when-all-the-values-are-in-one-column