Reputation: 133
I would like to write a function for a neat output table from a t-test as I'm conducting numerous post-hoc t-tests however, writing functions are not my strong suit so I'd like some help. My current code looks like this
library(tidyverse)
library(lsr)
library(broom)
t_table <- function(data$col, data$col) {
t.test(data$col, data$col) %>%
broom::tidy() %>%
mutate(Cohens_d = cohensD(data$col, data$col)) %>% # calc. cohen's d
mutate_at(vars(- c(p.value,method,alternative)), round, 2)
}
One of the errors is:
Error in data$col : object of type 'closure' is not subsettable.
I'm assuming data and col are not general names for any data frame and column.
Essentially I'd like to be able to specify any data frame and column for each variable. I'm not even sure if this is possible as it is a very general function I'm trying to create but any help would be much appreciated.
Upvotes: 0
Views: 708
Reputation: 11981
the input arguments for your function should a) not have the same name and b) should not contain $
. Other than that your function works fine:
t_table <- function(col1, col2) {
t.test(col1, col2) %>%
broom::tidy() %>%
mutate(Cohens_d = cohensD(col1, col2)) %>% # calc. cohen's d
mutate_at(vars(- c(p.value,method,alternative)), round, 2)
}
set.seed(1)
t_table(rnorm(100), rnorm(100)+1/2)
estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative Cohens_d
1 -0.35 0.11 0.46 -2.69 0.007745151 197.19 -0.61 -0.09 Welch Two Sample t-test two.sided 0.38
Upvotes: 1