1

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
     .... }
  }}
2
  • 1
    I'm not sure this is clear. You want to set SHP$Species to "A" for where 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.
    – Spacedman
    Commented Jun 14, 2021 at 20:11
  • Yeah I was confused whether to add this in stackoverflow or here. As for your questions, yes the column Species in SHP already exists, and yes, I want to leave the rest of the rows under Species that correspond to the TreeIDs mentioned in the conditions unchanged. Hope this makes sense, and do you want me to delete this question and repost in stackvoverflow? Commented Jun 14, 2021 at 20:18

1 Answer 1

2

Make a test data set with 26 rows, species from A to Z and tree ID random samples from 170 to 180:

> SHP = data.frame(species=LETTERS, treeID=sample(170:180,26,TRUE))
> SHP
   species treeID
1        A    173
2        B    174
3        C    175
4        D    176
5        E    172
6        F    171
7        G    171
8        H    172
9        I    173
10       J    173
11       K    172
12       L    175
13       M    178
14       N    174
15       O    173
16       P    171
17       Q    173
18       R    179
19       S    173
20       T    172
21       U    179
22       V    179
23       W    172
24       X    178
25       Y    180
26       Z    179

These are the ID variables you want to make not species A. I notice that 173 is missing.

> notSpeciesA = c(171, 172, 174, 175, 176, 177, 178, 179)

Now change the species column where the tree ID is not in that set:

> SHP$species[!(SHP$treeID %in% notSpeciesA)] = "A"

and check:

> SHP
   species treeID
1        A    173
2        B    174
3        C    175
4        D    176
5        E    172
6        F    171
7        G    171
8        H    172
9        A    173
10       A    173
11       K    172
12       L    175
13       M    178
14       N    174
15       A    173
16       P    171
17       A    173
18       R    179
19       A    173
20       T    172
21       U    179
22       V    179
23       W    172
24       X    178
25       A    180
26       Z    179
> 

173s and 180s are now "A". The rest unchanged. Making test data sets like this is a good thing to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.