lundi 14 décembre 2020

create a variable and add to a data frame within a loop in R using existing variable

I'm trying to add a variable to a data frame using a for loop in r. The for loop is supposed to run through an existing variable (NetMig) in the data frame (cs) and output a new variable (PNM). PNM is supposed to take a value of 1 if NetMig is greater than 0 and takes a value of 0 otherwise. Here is my code so far.

    for(i in 1:227) {
     if(cs$NetMig[i] > 0) {
        cs$PNM[i] <- 1
        else {
          cs$PNM[i] <- 0
        }
      }
    }

An error keeps popping up

Warning messages: 1: Unknown or uninitialised column: PNM. 2: Unknown or uninitialised column: PNM.

Can anyone show me what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire