0

I want a create a treatment variable that takes the value of 1 for treated countries and 0 otherwise. I know how to do this individually for each country with transform but I was wondering if I can also add a vector to make it faster? I tired the following but I got an error (Warning: longer object length is not a multiple of shorter object length)

countries_vector <- c("USA", "UK", "ESP", "FR", "ITA")

df <- transform(df, treated = ifelse(Country == countries_vector, 1, 0))
daviddupont
  • 135
  • 7
  • 1
    Please make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) or [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with a sample input and your expected output. – Martin Gal Dec 03 '22 at 20:33
  • 1
    Instead of `ifelse(Country == countries_vector, 1, 0)` try something like `+(Country %in% countries_vector)`. – Martin Gal Dec 03 '22 at 20:34

1 Answers1

2

You can use mutate, ifelse, and %in% to quickly assign 0 or 1.

library(dplyr)
treated_countries <- c("USA", "UK", "ESP", "FR", "ITA")
all_countries <- sample(x = c("USA", "UK", "ESP", "FR", "ITA", "BRA", "JAP", "CHN", "ZAF", "DEU"),
                  size = 100, replace = TRUE)
df <- as.data.frame(all_countries)
df <- df %>%
  mutate(treated = ifelse(all_countries %in% treated_countries, 1, 0))

df %>%
  group_by(treated, all_countries) %>%
  summarize()
#> `summarise()` has grouped output by 'treated'. You can override using the
#> `.groups` argument.
#> # A tibble: 10 × 2
#> # Groups:   treated [2]
#>    treated all_countries
#>      <dbl> <chr>        
#>  1       0 BRA          
#>  2       0 CHN          
#>  3       0 DEU          
#>  4       0 JAP          
#>  5       0 ZAF          
#>  6       1 ESP          
#>  7       1 FR           
#>  8       1 ITA          
#>  9       1 UK           
#> 10       1 USA
jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
  • Thank you! For some reason I am getting the following error "Warning: longer object length is not a multiple of shorter object length> ", do you know how to solve it? – daviddupont Dec 03 '22 at 22:17
  • I can only figure out how to solve it if you provide your data using `dput()`, as Martin Gal commented. If you're getting an error, that means it's most likely a problem with your dataset. – jrcalabrese Dec 03 '22 at 22:19