0

I have a shapefile uploaded as adataframe in R in which two of the columns are X1 and X5, and from these two columns, I am creating a new column class using dplyr. The code sort of works, but there are NA values in the new column, where there were suppose to be X1 values. How can this be fixed?

Sample data with columns of interest

FID X1  X5  class
1   VEG PRPU    
2   VEG PRPU    
3   VEG PRPU    
4   VEG PRPU    
5   WTR NA  
6   WTR NA  
7   WTR NA  
8   VEG PLSE    
9   VEG PLSE    
10  GRND NA         
11  GRND NA

    

Data type information using str(df)

Classes ‘sf’ and 'data.frame':  211 obs. of  8 variables:
 $ id      : num  NA NA NA NA NA NA NA NA NA NA ...
 $ X1      : chr  "VEG" "VEG" "VEG" "VEG" ...
 $ X2      : chr  "GRN" "GRN" "GRN" "GRN" ...
 $ X3      : chr  "SHRB" "SHRB" "SHRB" "SHRB" ...
 $ X4      : chr  "DES" "DES" "DES" "DES" ...
 $ X5      : chr  "PLSE" "PLSE" "PLSE" "PLSE" ...
 $ geometry:sfc_POLYGON of length 211; first list element: List of 1
  ..$ : num [1:7, 1:2] 756524 756524 756524 756524 756524 ...
  ..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
 $ class   : logi  NA NA NA NA NA NA ...
 - attr(*, "sf_column")= chr "geometry"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA
  ..- attr(*, "names")= chr [1:6] "id" "X1" "X2" "X3" ...
 

Code

library(tidyverse)
library(sf)
# Create a new column `class`
df = st_read("/path", "file")
df = df%>% add_column(class = NA)

# Mutate
df = df %>% dplyr::mutate(class = ifelse(X5 == "", X1, X5))

Updated spatial dataframe

  FID   X1  X5  class
    1   VEG PRPU PRPU   
    2   VEG PRPU PRPU
    3   VEG PRPU PRPU   
    4   VEG PRPU PRPU   
    5   WTR NA   NA
    6   WTR NA   NA
    7   WTR NA   NA
    8   VEG PLSE PLSE
    9   VEG PLSE PLSE   
    10  GRND NA  NA
    11  GRND NA  NA
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34
  • 2
    It's hard to tell from your data but could there be a space issue in the X5 column? I.e. `" " =/= ""`. You might try stripping whitespace off i.e. `str_trim()` or even try `coalesce` with X1 as the second argument. – Carey Caginalp Jul 16 '21 at 00:09
  • 1
    Can you please share your data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) such as a with a `dput()`? It's unclear exactly what you're working with. – MrFlick Jul 16 '21 at 00:10
  • `dput(df)` is creating a very long output, should I still paste it here? – Ed_Gravy Jul 16 '21 at 00:19

1 Answers1

0

Since there were NA values in the column X5 which are not exactly empty cells (as shown in ArcGIS). I had to slightly adjust the code, and now it works.

library(tidyverse)
library(sf)
# Create a new column `class`
df = st_read("/path", "file")
df = df%>% add_column(class = NA)

# Mutate
df = df %>% dplyr::mutate(class = ifelse(is.na(X5), X1, X5))
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34