Home > Blockchain >  Why is my code returning all NA's when I operate on a string in R?
Why is my code returning all NA's when I operate on a string in R?

Time:11-02

Whenever I refer to any string data in my df it returns NA's as if that string does not exist but it's not a spelling error on my part.

I was doing some basic sorting functions on my data, all of which I've done before in the same R script on other datasets, and for some reason it is not recognizing my string data.

> head(Vg)
               Sp            key  value
1          Salal  Thinned _ CAR1 18.500
2        Bracken  Thinned _ CAR1  1.275
3 RedHuckleberry  Thinned _ CAR1  0.550
4      SwordFern  Thinned _ CAR1  0.500
5       DeerFern  Thinned _ CAR1  0.500
6       LadyFern  Thinned _ CAR1  0.000
> V_VON<-Vg[grepl("VON1",Vg$key),]
> V_VON<-V_VON[!V_VON$value==0,]
> V_VON
                        Sp             key     value
289                 Salal  Clearcut _ VON1  4.666667
290               Bracken  Clearcut _ VON1  0.500000
291        RedHuckleberry  Clearcut _ VON1  0.500000
292             SwordFern  Clearcut _ VON1  0.500000
293              DeerFern  Clearcut _ VON1  0.500000
295           Salmonberry  Clearcut _ VON1  0.500000
296      TrailingRasberry  Clearcut _ VON1  0.500000
297    TrailingBlackberry  Clearcut _ VON1  0.500000
302              RyeGrass  Clearcut _ VON1  0.500000
303 ThreeLeavedFoamFlower  Clearcut _ VON1  0.500000
305           BaldhipRose  Clearcut _ VON1  0.500000
307               UGrass1  Clearcut _ VON1  0.500000
316         Redwoodviolet  Clearcut _ VON1  0.500000
323              Fireweed  Clearcut _ VON1  0.500000
325                 Salal   Control _ VON1 60.000000
326               Bracken   Control _ VON1  0.500000
327        RedHuckleberry   Control _ VON1  0.500000
328             SwordFern   Control _ VON1  0.500000
> V_VON$key <- factor(V_VON$key, levels = c("Control_VON1","Clearcut_VON1"))
> V_VON
                        Sp  key     value
289                 Salal  <NA>  4.666667
290               Bracken  <NA>  0.500000
291        RedHuckleberry  <NA>  0.500000
292             SwordFern  <NA>  0.500000
293              DeerFern  <NA>  0.500000
295           Salmonberry  <NA>  0.500000
296      TrailingRasberry  <NA>  0.500000
297    TrailingBlackberry  <NA>  0.500000
302              RyeGrass  <NA>  0.500000
303 ThreeLeavedFoamFlower  <NA>  0.500000
305           BaldhipRose  <NA>  0.500000
307               UGrass1  <NA>  0.500000
316         Redwoodviolet  <NA>  0.500000
323              Fireweed  <NA>  0.500000
325                 Salal  <NA> 60.000000
326               Bracken  <NA>  0.500000
327        RedHuckleberry  <NA>  0.500000
328             SwordFern  <NA>  0.500000

I thought it might be some error with the underscore so I tried with values in the "Sp" column but the same thing happens.

> V_VON1 <- V_VON[V_VON$Sp=="Salal", ] 
> V_VON1
[1] Sp    key   value
<0 rows> (or 0-length row.names)

I can, however, operate normally on the numeric column.

> V_VON2 <- V_VON[V_VON$value==0.5,]
> V_VON2
                        Sp             key value
290               Bracken  Clearcut _ VON1   0.5
291        RedHuckleberry  Clearcut _ VON1   0.5
292             SwordFern  Clearcut _ VON1   0.5
293              DeerFern  Clearcut _ VON1   0.5
295           Salmonberry  Clearcut _ VON1   0.5
296      TrailingRasberry  Clearcut _ VON1   0.5
297    TrailingBlackberry  Clearcut _ VON1   0.5
302              RyeGrass  Clearcut _ VON1   0.5
303 ThreeLeavedFoamFlower  Clearcut _ VON1   0.5
305           BaldhipRose  Clearcut _ VON1   0.5
307               UGrass1  Clearcut _ VON1   0.5
316         Redwoodviolet  Clearcut _ VON1   0.5
323              Fireweed  Clearcut _ VON1   0.5
326               Bracken   Control _ VON1   0.5
327        RedHuckleberry   Control _ VON1   0.5
328             SwordFern   Control _ VON1   0.5

Any ideas what might be causing this? My only thought is maybe something with the grepl function because its the first time I've used it.

CodePudding user response:

There could be leading/lagging spaces which can be removed with trimws as == looks for fixed match

V_VON[trimws(V_VON$Sp)=="Salal", ]
  •  Tags:  
  • r
  • Related