I have a very large dataset and want to write an economical code for data analysis.
Here is an example for illustration
df <- data.frame(
ID = factor(sample(c("A","B","C","D","E","F","G"), 20, replace=TRUE)),
a1 = runif(20),
a2 = runif(20),
a3 = runif(20),
a4 = runif(20),
b1 = runif(20),
b2 = runif(20),
b3 = runif(20),
b4 = runif(20))
I would like to make a paired samples t test like this (example):
t.test(df$a1, df$b1, paired=TRUE, na.rm=TRUE)
t.test(df$a2, df$b2, paired=TRUE, na.rm=TRUE)
That works but I want a shorter code and tried that:
object_a <- paste("a", 1:4, sep="")
object_b <- paste("b", 1:4, sep="")
t.test.func.paired <- function(x) {
t.test(x, y, paired = TRUE, na.rm=TRUE)
}
df %>%
select_(.dots = c(object_a, object_b)) %>%
sapply(., t.test.func.paired) %>%
.[c("statistic", "parameter", "p.value"), ] %>%
View()
Unfortunately that does not work. But where is the error? Thank you!