Home > Software engineering >  Loop filter data frame and export resulting subsets
Loop filter data frame and export resulting subsets

Time:04-06

I have the following dataset:

df <- data.frame(CountryCode = c("AT", "AT", "DE", "DE", "UK", "UK", "NZ", "NZ"),
  GDP = c("120", "125", "300", "320", "250", "265", "50", "60"),
  Year = c("2009", "2010","2009", "2010","2009", "2010","2009", "2010")
)

I want to filter using a loop based on each unique value in column CountryCode and assign each subset to an own unique dataframe. What is the most concise way to do so?

CodePudding user response:

You can use split for that:

split(df, df$CountryCode)
$AT
#   CountryCode GDP Year
# 1          AT 120 2009
# 2          AT 125 2010

# $DE
#   CountryCode GDP Year
# 3          DE 300 2009
# 4          DE 320 2010

# $NZ
#   CountryCode GDP Year
# 7          NZ  50 2009
# 8          NZ  60 2010

# $UK
#   CountryCode GDP Year
# 5          UK 250 2009
# 6          UK 265 2010


CodePudding user response:

Another possible solution, based on dplyr:

library(dplyr)

df %>% group_by(CountryCode) %>% group_split()

#> <list_of<
#>   tbl_df<
#>     CountryCode: character
#>     GDP        : character
#>     Year       : character
#>   >
#> >[4]>
#> [[1]]
#> # A tibble: 2 × 3
#>   CountryCode GDP   Year 
#>   <chr>       <chr> <chr>
#> 1 AT          120   2009 
#> 2 AT          125   2010 
#> 
#> [[2]]
#> # A tibble: 2 × 3
#>   CountryCode GDP   Year 
#>   <chr>       <chr> <chr>
#> 1 DE          300   2009 
#> 2 DE          320   2010 
#> 
#> [[3]]
#> # A tibble: 2 × 3
#>   CountryCode GDP   Year 
#>   <chr>       <chr> <chr>
#> 1 NZ          50    2009 
#> 2 NZ          60    2010 
#> 
#> [[4]]
#> # A tibble: 2 × 3
#>   CountryCode GDP   Year 
#>   <chr>       <chr> <chr>
#> 1 UK          250   2009 
#> 2 UK          265   2010
  • Related