GingerOverlord GingerOverlord - 1 month ago 9
R Question

R: using t.test in ddply on different columns in same data frame

I'm new to R and would like to know how to apply the t.test function to different columns in the same data frame inside of ddply and also save those p.values. I don't know if it's possible, but I would love fpr t to work.

Using mtcars as an example, try tpo imagine that there were multiple mpg columns in mtcars, one for each year of that make and model to track any changes in the efficiency of the car or something. I'd like to use ddply to apply the t.test function to these different mpg columns and save the t.test results to the same data frame.

I already have an uber simple function that would take two columns and apply t.test to them, but I don't know how to save the result into the data frames. Here is the code:

ttest <- function(df1, df2){
df.1 <- t.test(df1, df2)
return(df.1)
}


I'm new t R, again, so I may be overlooking easier ways to do this.

Answer

i would read some intro stats books in R to get a good understanding of linear modelling, how to wield it and how to interpret the output.

ill show a simple example of modelling a continuous variable as a function of a continuous variable and a factor.

data(iris)

use the lm function to fit the model

mod <- lm(Sepal.Length ~ Sepal.Width + Species, iris)

use anova and print to inspect the results

anova(mod)
mod