Let's say I want to compare the price of apples and oranges in each country in two different currencies: USA and BTC.
USA ~ fruit for each country
BTC ~ fruit for each country
library(tidyverse)
prices <- tibble(
country = c(rep("USA", 6), rep("Spain", 6), rep("Korea", 6)),
fruit = rep(c("apples", "apples", "apples", "oranges", "oranges", "oranges"), 3),
price_USA = rnorm(18),
price_BTC = rnorm(18)
)
prices %>%
group_by(country) %>%
summarise(
pval_USA = t.test(price_USA ~ fruit)$p.value
pval_BTC = t.test(price_BTC ~ fruit)$p.value
)
Now let's say there are many columns and I want to use summarise_all instead of naming each column. Is there a way to perform a t-test within each group (country) and on each column (price_USA, price_BTC) using the dplyr::summarise_all function? The approaches I've tried so far have been giving me errors.
prices %>%
group_by(country) %>%
summarise_at(
c("price_USA", "price_BTC"),
function(x) {t.test(x ~ .$fruit)$p.value}
)
> Error in model.frame.default(formula = x ~ .$fruit) :
variable lengths differ (found for '.$fruit')