Kevin23
Kevin23

Reputation: 1

How do I select multiple data on R

I have country data of natural disasters and economical loss. I am trying to see whether climate change intensifies natural disasters over time. I am trying to group together natural disasters caused by climate change (Wildfire', 'Drought', 'Landslide', 'Flood') and natural disasters that are not ('Earthquake')

This is the code I have carried out:

climate = c('Wildfire', 'Drought', 'Landslide', 'Flood', 'Earthquake')
newdata$climate_related = ifelse(newdata$Entity %in% climate, "yes", "yes","yes","yes","no")

This is the Error I receive:

Error in ifelse(newdata$Entity %in% climate, "yes", "yes", "yes", "yes", : unused arguments ("yes", "yes", "no")

What am I doing wrong? And, what is the best way to carry this out?

Upvotes: 0

Views: 49

Answers (1)

akrun
akrun

Reputation: 887048

We need just a 'no' as else as the ifelse takes only three arguments i.e. test - logical expression, yes - the value to replace if the condition is TRUE, no- value to replace if condition is FALSE. Also, all the arguments should be of the same length. But, if the length is 1, it gets recycled

ifelse(newdata$Entity %in% climate, "yes", "no")

One option if there are many elements, either collapse them as a single string

no_option <- toString(c("yes","yes","yes","no"))
ifelse(newdata$Entity %in% climate, "yes", no_option)

Or create a list and return as list

no_option <- list("yes","yes","yes","no")

Upvotes: 2

Related Questions