mercredi 31 mars 2021

Create a new column from conditions

I have a dataframe with information of some countries and states like this:

data.frame("state1"= c(NA,NA,"Beijing","Beijing","Schleswig-Holstein","Moskva",NA,"Moskva",NA,"Berlin"), 
               "country1"=c("Spain","Spain","China","China","Germany","Russia","Germany","Russia","Germany","Germany"),
"state2"= c(NA,NA,"Beijing",NA,NA,NA,"Moskva",NA,NA,NA), 
"country2"=c("Germany","Germany","China","Germany","","Ukraine","Russia","Germany","Ukraine",""  ),
"state3"= c(NA,NA,NA,NA,"Schleswig-Holstein",NA,NA,NA,NA,"Berlin"), 
               "country3"=c("Spain","Spain","Germany","Germany","Germany","Germany","Germany","Germany","Germany","Germany"))

Now, I would like to create a new column with the information of German states. (the result would look like below). When at least one of the three variables state are a German state, assign it in the new variable.

data.frame("GE_State"=c(NA,NA,NA,NA, "Schleswig-Holstein",NA,NA,NA,NA,"Berlin"))

Please help a beginner for the condition setting. Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire