0

I have the following fake-dataset, very similar to the real one:

Country <- c("Italy", "Italy", "Germany", "Germany", "Spain", "Spain", "France", "France")
Specialisation <- c("Cooperative", "Savings", "Cooperative", "Savings", "Cooperative", "Savings", "Cooperative", "Savings" )
TotalAssets <- c(12, 3, 45, 6, 34, 98, 23, 5)
df <- data.frame(Country = Country, Specialisation = Specialisation, TotalAssets = TotalAssets)

I want to calculate the differences in mean between the Total Assets of Savings and Cooperative banks, for each country.

This is the code I came up with, but it does not work.

t <- df %>% group_by(Country) %>% t.test(TotalAssets ~ Specialisation, alternative = "two.sided", var.equal = FALSE)

UPDATE:

I tried to use the code above belonging to @SALAR. I get the following error:

    Error in t.test.formula(TotalAssets ~ Specialisation, alternative = "two.sided", : grouping factor must have exactly 2 levels

I transformed the "Specialisation" column in factor:

TotalAsset_df$Specialisation <- as.factor(TotalAsset_df$Specialisation)

I checked the levels:

levels(TotalAsset_df$Specialisation)

Output:

[1] "Cooperative bank" "Savings bank"

But still, when I execute the code, I get the following error:

Thanks to whoever is gonna help!

1 Answers1

0

You can split data by country and use 'map' function to loop over:

library(purrr)

t<-df %>% split(.$Country) %>% 
map (.,~ t.test(TotalAssets ~ Specialisation, alternative = "two.sided", 
var.equal = FALSE,data = .))

Then extract p.values (or any statistic you may want) for each country:

map_dbl(t,~.$"p.value")
S-SHAAF
  • 1,863
  • 2
  • 5
  • 14
  • thanks for your reply. But executing your code I get the following error: Error in t.test.formula(TotalAssets ~ Specialisation, alternative = "two.sided", : grouping factor must have exactly 2 levels. DId it happen to you too? – Martina Noli Maio Oct 29 '22 at 20:57
  • No, Probably your grouping variable has more than two levels for the grouping factor 'Specialisation' from a country or countries. Please carefully check your data for the number of levels. If you see more than two, correct them. – S-SHAAF Oct 29 '22 at 22:17
  • I see, I have 23 countries, but Specialisation can be only "Cooperative Bank" or "Saving Bank".. Can I kindly ask you what is meant for "levels".. I read some information on internet.. but it' s not clear to me. Thanks – Martina Noli Maio Oct 30 '22 at 09:37
  • Can you check the number of levels by this code? first convert to factor using : data$Specialisation<-factor(data$Specialisation). Then check levels using : levels(data$Specialisation). You must have two levels ("Cooperative Bank" or "Saving Bank"), if not correct them. – S-SHAAF Oct 30 '22 at 15:54
  • HI, thanks for coming back! I did this check and I updated the question above. They are effectively 2 levels. I do not really understand why this does not work. I read dozen of question in and out stackoverflow! – Martina Noli Maio Oct 30 '22 at 16:12