0

I have a list of Noxious Weed species in California and a table with all of the species ever seen in a certain site. I want to create a column in the table that will denote which species are Noxious Weeds.

I've been hitting dead ends with this all day and I'm not sure how to continue!

  • Welcome to Stack Overflow! In the future it will be best to have a reproducible example, but this one is simple. You just use `<-` (the assignment operator), i.e. `df$new_col <- ifelse(test, 1,2)` – Hack-R Apr 20 '20 at 22:29

1 Answers1

3
data(iris)
head(iris)

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

iris$new_col <- ifelse(iris$Species=="setosa",1,0)
head(iris)

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species new_col
1          5.1         3.5          1.4         0.2  setosa       1
2          4.9         3.0          1.4         0.2  setosa       1
3          4.7         3.2          1.3         0.2  setosa       1
4          4.6         3.1          1.5         0.2  setosa       1
5          5.0         3.6          1.4         0.2  setosa       1
6          5.4         3.9          1.7         0.4  setosa       1
Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • I started to close this as a dupe but didn't see a good link to use to close it with. If someone else wants to close it as a dupe that's fine. – Hack-R Apr 20 '20 at 22:33