lundi 22 juin 2020

For loops using grepl

I am trying to use a for loop to run through a data frame, see if an observation contains a certain string in a column (i.e., it should contain "no law" in the column Content), and generate values in a different column based on the outcome.

If it does contain the string, which is identified by the outcome of the grepl function being True, then the observation should have 'Permissive' in the Effectrp column; otherwise, it should say 'Restrictive'.

I'm not quite sure what I'm doing wrong... Any help would be appreciated!

for (i in 1:nrow(ldb)){
  if (grepl('no law', ldb$Content[i], ignore.case = TRUE)) == TRUE {
    ldb$Effectrp[i] = 'Permissive'
  } else {
    lab$EffectTR[i] = 'Restrictive'
  }
}

Aucun commentaire:

Enregistrer un commentaire