New code variable based on grep return in R

I have an actor variable, which is a string and contains values โ€‹โ€‹similar to "military forces of guinea-bissau (1989-1992)" , and a large number of other different values โ€‹โ€‹that are quite complex. I used grep() to search for character templates that match different types of participants. For example, I would like to encode the new variable actor_type as 1 , when actor contains "military forces of" , does not contain "mutiny of" , and the string variable country also contained in the variable actor .

I donโ€™t understand how to conditionally create this new variable without resorting to some kind of terrible cycle. Help me!

The data looks something like this:

 | | actor | country | |---+----------------------------------------------------+-----------------| | 1 | "military forces of guinea-bissau" | "guinea-bissau" | | 2 | "mutiny of military forces of guinea-bissau" | "guinea-bissau" | | 3 | "unidentified armed group (guinea-bissau)" | "guinea-bissau" | | 4 | "mfdc: movement of democratic forces of casamance" | "guinea-bissau" | 
+4
source share
1 answer

if your data is in data.frame df:

 > ifelse(!grepl('mutiny of' , df$actor) & grepl('military forces of',df$actor) & apply(df,1,function(x) grepl(x[2],x[1])),1,0) [1] 1 0 0 0 

grepl returns a logical vector, and this can be assigned to anyone, for example. df$actor_type .

violation of this application:

!grepl('mutiny of', df$actor) and grepl('military forces of', df$actor) satisfy your first two requirements. the last fragment of apply(df,1,function(x) grepl(x[2],x[1])) goes along the lines and greps for the country in the actor.

+5
source

Source: https://habr.com/ru/post/1394796/


All Articles