Creating column based on criteria

I'm new to R so I'm having trouble organizing my data I have a data frame with various occurrences of about 200 different species and their location. Something like this

                   Species Longitude Latitude 
       1 Abronia graminea    -97.30    18.72   
       2 Abronia graminea    -97.33    18.70    
       3 Abronia graminea    -97.03    19.65     
       4 Abronia graminea    -97.10    18.85   
       5 Abronia graminea    -97.03    19.61    
       6 Abronia graminea    -97.34    18.69

What I want to do is create a new column with info for "type" of each species. I have another table with the information of the type for each species that looks like this:

           Species            Type
 1     Abronia graminea    lizard-like                 
 2     Acontias litoralis  short-tailed
 3     Anguis fragilis     long-tailed
 4     Anilius scytale     short-tailed

It's hard to be certain without seeing the data frame with the type information, but it sounds like a "join" might work here. For example, using the built-in iris data frame:

library(tidyverse)

# Fake data frame with "type" information that we want to add to iris
type.data = data.frame(Species=sort(unique(iris$Species)), 
                       Type = c("A","B","C"))


iris = left_join(iris, type.data, by="Species")

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

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.