How to create vectors of countries from gapminder database in R?

I would be really grateful if someone could help me with creating vectors of countries like in the snapshot attached, from gapminder database in R?

Thx

You could convert it to a list like in the example bellow, altough I see no practical purpouse on this, could you explain why you wan to do this?

library(gapminder)
library(tidyverse)

as_list <- gapminder %>% 
  group_split(country) %>% 
  set_names(unique(gapminder$country))

as_list$Angola$year
#>  [1] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 2002 2007

Created on 2019-12-10 by the reprex package (v0.3.0)

I want to understand better how to manipulate with data in R so this is why I asked this question. I am trying to replicate everything I learned from R Studio Primers and there were couple of exercises with map function on preformatted gapminder and no matter how hard I tried I could not achieve this. Thank you.

Using base R this would be a similar output but split differently:

library(gapminder)

lapply(gapminder, split, gapminder$country)
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.