cms
cms

Reputation: 83

How to create a new column based on partial string of another column

I have a data frame with a vector of thousands of project codes, each representing a different type of research. Here's an example:

Data <- data.frame(Assignment = c("C-209", "B-543", "G-01", "LOG"))

The first letter of the assignment code denotes the type of research. C = Cartography, B = Biology, G = Geology, and LOG = Logistics.

I would like to create a new column that looks at the first letter of the Assignment column and uses it to denote the type of research it is.

I've tried something similar to this thread, but I know I'm missing something:

R - Creating New Column Based off of a Partial String

Data <- data.frame(Assignment = c("C-209", "B-543", "G-01", "LOG"))

Types <- data.frame(Type = c("Cartography", "Biology", "Geology","Logistic"), 
                 stringsAsFactors = FALSE)

Data %>% 
  mutate(Type = str_match(Assignment, Types$Type)[1,])

Upvotes: 3

Views: 4060

Answers (1)

Vishal Katti
Vishal Katti

Reputation: 652

You can add a new column Code in your Types data.frame and then join it with original table. You will need to create a Code column in your Data data.frame too.

    library(dplyr)
    library(stringr)

    Data <- data.frame(Assignment = c("C-209", "B-543", "G-01", "LOG"))

    Types <- data.frame(Type = c("Cartography", "Biology", "Geology","Logistic"),
                    Code = c("C","B","G","L"), # Create new column here
                    stringsAsFactors = FALSE)


    Data <- Data %>% mutate(Code = substr(Assignment,1L,1L)) # extract first character

    Data <- left_join(Data, Types, by = "Code") %>% select(Assignment, Type) # combine

Upvotes: 5

Related Questions