mercredi 14 décembre 2016

ifelse resulting in NA for some levels with special characters

DATA

Please follow this link to find the .dbf file.

I imported it into R as below

library(foreign)
LRI_rocks <- read.dbf("LRI_rocks.dbf")

As you can see, below, in the levels of column Top_Rock There are some levels that have — inside them.

> levels(LRI_rocks$Top_Rock)
 [1] "Ashes older than Taupo pumice"                                                   
 [2] "Gravels"                                                                         
 [3] "Greywacke"                                                                       
 [4] "Kaharoa & Taupo ashes"                                                           
 [5] "Loess"                                                                           
 [6] "Mudstone or fine siltstone —  jointed"                                         
 [7] "Mudstone or fine siltstone — massive"                                          
 [8] "Ngauruhoe ash"                                                                   
 [9] "Peat"                                                                            
[10] "River"                                                                           
[11] "Sands — windblown"                                                             
[12] "Sandstone or coarse siltstone — massive"                                       
[13] "Taupo & Kaharoa breccia & volcanic alluvium"                                     
[14] "town/urbanarea/oxidationpond"                                                    
[15] "Unconsolidated to moderately consolidated clays, silts, sands, tephra & breccias"
[16] "Undifferentiated floodplain alluvium"

I want to convert the levels of Top_Rock to numbers as below

library(dplyr)
df <-  LRI_rocks %>% 
  dplyr::select(Top_Rock) %>% 
  dplyr::mutate(top_rock_no = ifelse(Top_Rock =="Ashes older than Taupo pumice", 1, 
                                     ifelse(Top_Rock == "Gravels", 1, 
                                            ifelse(Top_Rock == "Greywacke", 1, 
                                                   ifelse(Top_Rock == "Kaharoa & Taupo ashes", 1, 
                                                          ifelse(Top_Rock == "Loess", 1, 
                                                                 ifelse(Top_Rock == "Mudstone or fine siltstone —  jointed", 3,
                                                                        ifelse(Top_Rock == "Mudstone or fine siltstone — massive", 3,
                                                                               ifelse(Top_Rock == "Ngauruhoe ash", 1,
                                                                                      ifelse(Top_Rock == "Peat", 3,
                                                                                             ifelse(Top_Rock == "Sands — windblown", 1,
                                                                                                    ifelse(Top_Rock == "Sandstone or coarse siltstone — massive", 2,
                                                                                                           ifelse(Top_Rock == "Taupo & Kaharoa breccia & volcanic alluvium", 2,
                                                                                                                  ifelse(Top_Rock == "Unconsolidated to moderately consolidated clays, silts, sands, tephra & breccias", 3,
                                                                                                                         ifelse(Top_Rock == "Undifferentiated floodplain alluvium", 2, NA)
                                                                                                                  ))))))))))))))

All levels with — have been replaced with NA.

QUESTION

Any suggestion how to convert levels with — to the numbers I have assigned in ifelse instead of having them as NA?

Aucun commentaire:

Enregistrer un commentaire