mardi 9 novembre 2021

For - loop over rows and columns, filling an empty dataframe, depending on values of another dataframe (r)

I try to fill an empty dataframe (pen_NA) with values of 1 - 3 depending on the height of the values in another data frame (pen_2). There is no error message, but the code does not do the job either. Are there some hints?

My data frame looks like this: Trait1 Trait2 ... Plant.nr.1 value value ... Plant.nr.2 value value ... ...

for (j in 1:ncol(pen_NA)) {

 for (i in which(is.na(pen_NA[,j]))) { # https://stackoverflow.com/questions/48403462/r-assign-value-based-on-criteria
  
    if (pen_2[i,j] > 3) 
  {pen_NA[i,j]== 1}
  else if (pen_2[i,j] < 3)
  {pen_NA[i,j]== 2}
  else if (pen_2[i,j] == 3)  
  {pen_NA[i,j]== 3}
  else
  pen_NA[i,j]== pen_NA[i,j]
 }}

Aucun commentaire:

Enregistrer un commentaire