user3227641
user3227641

Reputation: 177

creating a conditional dummy variable using dplyr and ifelse statements in R

I'm working with a panel data frame. I wanted to create a dummy variable that takes a value of 1 if the average value of the variable named "manu_GDP" is greater than or equal to 20 for each country in my dataset. I did this using the code below.

dummy <- df %>% 
                group_by(country) %>%
                summarise(avg=mean(manu_GDP, na.rm=TRUE)) %>% 
                mutate(high_manu=ifelse(avg >= 20, 1, 0)) 

Now I want to create an alternative dummy variable that would take the value 1 if the value of manu_GDP (not its average over time) is greater than 20 in any given year for each country. For instance, the average value of manu_GDP could be less than 20 for Ethiopia but Ethiopia might have recorded a value of manu_GDP greater than 20 for a few years. I would like my alternative dummy variable to be able to pick this up.

Any advice on how I can do this?

Thanks very much in advance.

Upvotes: 1

Views: 1403

Answers (2)

akrun
akrun

Reputation: 887108

We could also do

library(dplyr)
df %>%
    group_by(country) %>%
    summarise(new_dummy = +(sum(manu_GDP > 20, na.rm = TRUE) > 0))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388982

You can use any to check if any value of manu_GDP is greater than 20.

library(dplyr)

df %>% 
  group_by(country) %>%
  summarise(new_dummy = as.integer(any(manu_GDP > 20, na.rm = TRUE)))

If you want to maintain the number of rows in the data use mutate instead of summarise.

Upvotes: 1

Related Questions