{r} unique(ess$cntry)

## Task 1

Provide code and answer.

Prompt and question: calculate the average for the variable ‘happy’ for the country of Norway. On average, based on the ESS data, who reports higher levels of happiness: Norway or Belgium?

Note: we already did it for Belgium. You just need to compare to Norway’s average, making sure to provide the code for both.

{r} belgium_happy <- ess %>% filter(cntry == "BE") %>% select(happy)

```{r} belgium_happy\(y <- belgium_happy\)happy

table(belgium_happy$y)



```{r}
belgium_happy$y[belgium_happy$y %in% 77:99] <- NA

# checking again
table(belgium_happy$y)

{r} belgium_happy %>% summarize( mean_y = mean(y, na.rm = TRUE), median_y = median(y, na.rm = TRUE) ) %>% print()

{r} norway_happy <- ess %>% filter(cntry == "NO") %>% select(happy)

```{r} norway_happy\(y <- norway_happy\)happy

table(norway_happy$y)


```{r}
norway_happy$y[norway_happy$y %in% 77:99] <- NA

# checking again
table(norway_happy$y)

{r} norway_happy %>% summarize( mean_y = mean(y, na.rm = TRUE), median_y = median(y, na.rm = TRUE) ) %>% print() #Answer:Norway reports “7.97” happiness, which is “0.24” higher than Belgium’s “7.73”. As a result, Norwegians are happier than Belgians.

Task 2

Provide code and answer.

Prompt and question: what is the most common category selected, for Irish respondents, for frequency of binge drinking? The variable of interest is: alcbnge.

More info here: https://ess-search.nsd.no/en/variable/0c65116e-7481-4ca6-b1d9-f237db99a694.

Hint: need to convert numeric value entries to categories as specified in the variable information link. We did similar steps for Estonia and the climate change attitude variable.

{r} ireland_ccdrinking <- ess %>% filter(cntry == "IL") %>% select(alcbnge)

```{r} ireland_ccdrinking\(y <- ireland_ccdrinking\)alcbnge

table(ireland_ccdrinking$y)


```{r}
ireland_ccdrinking$y[ireland_ccdrinking$y %in% 6:8] <- NA

table(ireland_ccdrinking$y)

```{r} mean_y <- mean(ireland_ccdrinking\(y, na.rm = TRUE) median_y <- median(ireland_ccdrinking\)y, na.rm = TRUE)

cat(“Mean of ‘y’:”, mean_y, “”)



```{r}
cat("Median of 'y':", median_y, "\n")

```{r} df <- ireland_ccdrinking %>% mutate( y_category = case_when( y == 1 ~ “Don’t Binge Drink”, y == 2 ~ “Rarely Binge Drink”, y == 3 ~ “Somewhat Binge Drink”, y == 4 ~ “Frequently Binge Drink”, y == 5 ~ “Extreme Binge Drink”, TRUE ~ NA_character_ ), y_category = fct_relevel(factor(y_category),

                         "Don't Binge Drink", 
                         "Rarely Binge Drink", 
                         "Somewhat Binge Drink", 
                         "Frequently Binge Drink", 
                         "Extreme Binge Drink")

)

```{r}
table(df$y_category)

```{r} get_mode <- function(v) { tbl <- table(v) mode_vals <- as.character(names(tbl)[tbl == max(tbl)]) return(mode_vals) }

mode_values <- get_mode(df$y_category) cat(“Mode of y category:”, paste(mode_values, collapse = “,”), “”)

#Answer: Irish respondents prefer "Extreme Binge Drinking" as their primary category for binge drinking.


## Task 3

Provide code and answer. 

Prompt and question: when you use the summary() function for the variable plnftr (about planning for future or taking every each day as it comes from 0-10) for both the countries of Portugal and Serbia, what do you notice? What stands out as different when you compare the two countries (note: look up the variable information on the ESS website to help with interpretation)? Explain while referring to the output generated.

```{r}
result <- ess %>%
  
  filter(cntry %in% c("PT", "SE")) %>%
  

  mutate(plnftr = recode(plnftr, `77` = NA_real_, `88` = NA_real_, `99` = NA_real_)) %>%
  
  
  group_by(cntry) %>%
  summarize(mean_plnftr = mean(plnftr, na.rm = TRUE))

print(result)

#Answer: Portugal leans somewhat more towards the “planning for future” approach than Serbia, with averages of 5.41 and 5.01, respectively.

Task 4

Provide code and answer.

Prompt and question: using the variables stfdem and gndr, answer the following: on average, who is more dissastified with democracy in Italy, men or women? Explain while referring to the output generated.

Info on variable here: https://ess.sikt.no/en/variable/query/stfdem/page/1

```{r} italy_data <- ess %>% filter(cntry == “IT”)

italy_data <- italy_data %>% mutate( gndr = case_when( gndr == 1 ~ “Male”, gndr == 2 ~ “Female”, TRUE ~ as.character(gndr) ), stfdem = ifelse(stfdem %in% c(77, 88), NA, stfdem) )


```{r}
means_by_gender <- italy_data %>%
  group_by(gndr) %>% 
  summarize(stfdem = mean(stfdem, na.rm = TRUE))

print(means_by_gender)

#Answer:The research shows that males in Italy are more satisfied with democracy than women, with an average score of 4.78.

Task 5

Provide code and answer.

Prompt: Interpret the boxplot graph of stfedu and stfhlth that we generated already: according to ESS data, would we say that the median French person is more satisfied with the education system or health services? Explain.

Change the boxplot graph: provide the code to change some of the key labels: (1) Change the title to: Boxplot of satisfaction with the state of education vs. health services; (2) Remove the x-axis label; (3) Change the y-axis label to: Satisfaction (0-10).

Hint: copy the boxplot code above and just replace or cut what is asked.

{r} france_data <- ess %>% filter(cntry == "FR")

{r} france_data %>% # Setting values to NA mutate(stfedu = ifelse(stfedu %in% c(77, 88, 99), NA, stfedu), stfhlth = ifelse(stfhlth %in% c(77, 88, 99), NA, stfhlth)) %>% # Reshaping the data select(stfedu, stfhlth) %>% gather(variable, value, c(stfedu, stfhlth)) %>% # Creating the boxplot ggplot(aes(x = variable, y = value)) + geom_boxplot() + labs(y = "Satisfaction (0-10) ", title = "Boxplot of satisfaction with the state of education vs. health services") + theme_minimal()

the chart is not showing when i knit it but is showing in rstudios thanks