vendredi 27 juillet 2018

How to create a data frame from a nested for loop

I have a data frame which contains 341785 observations, imported as a .csv. I am running one column of my data through a nested for/if statement. I want to create an output in the form of a data frame, at the moment it is just filling the console with the output, row by row.

`for (level1 in dim(st) {
  if (is.na(st$level1[i])){
    print("No Level 1")
  }else if(st$level1[i]=="A"){
     print("Woodland")
  }else if (st$level1[i]=="B") {
     print("Scrubland")
  }else if(st$level1[i]=="C") {
     print("Semi-Natural Grassland/Marsh")
  }else if (stb$level1[i]=="D") {
     print("Unknown")
  }else if (st$level1[i]=="E") {
    print("Farmland")
  }else if(st$level1[i]=="F") {
    print("Human Sites")
  }else if (st$level1[i]=="G") {
    print("Water Bodies (Freshwater)")
  }else print("Other")
 }`

Goal would be

Level 1 1 Woodland 2 Farmland 3 Unknown 4 No Level 1 5 ...

I have tried to do it lots of different ways suggested online but none of them have worked, they have only created data frames with 0 Levels and 0 Observations. I am new to R and need help, especially as I am going to need to reproduce this for at least 6 more columns...

Aucun commentaire:

Enregistrer un commentaire