简体   繁体   中英

Create a new column in a spatial dataframe from existing columns in R

I have a shapefile uploaded as a dataframe 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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM