I have a shapefile that has multiple columns, including TreeID
and Species
. I am trying to add data to the Species column such that all rows under Species should have value A
, when TreeID !=
171, 172, 174, 175, 176, 177, 178 & 179
. So, basically, I want to add A as species to the rest of the TreeIDs. Below is what I have tried so far using loops and conditions, but I am stuck. How can I do this in a more tidy way using tidyverse
?
library(tidyverse)
library(rgdal)
library(sf)
## Add the species name to the species column in the shapefile
# Bring in the shapefile
SHP = read_sf("fiel_path", "file_name")
# Now add the species name to the species column
for{i in SHP$treeID
ifelse( i == 171 | i == 172 i == 174 | i == 175 i == 176 | i == 177 i == 178 | i == 179)
{
for{x in SHP$Species
.... }
}}
i
is in that range - but does SHP$Species already exist? DO you want to leave the rest of the rows unchanged? This is a probably a one line (SHP[condition,"Species"] = values
) in base R. Can you clarify? Also its not really a GIS question since this is also the way you'd deal with plain non-spatial data frames.stackoverflow
or here. As for your questions, yes the columnSpecies
inSHP
already exists, and yes, I want to leave the rest of therows
underSpecies
that correspond to theTreeIDs
mentioned in the conditions unchanged. Hope this makes sense, and do you want me to delete this question and repost instackvoverflow
?