Afrida Shama
Afrida Shama

Reputation: 67

Separate numeric and character data from a column in to two columns

I would like to separate the values of the "Code" column into two columns named "Country_Code" and "Product_Code" where the country code will be a character vector(such as AE, BA, UAE) and the product code will be numeric (such as 0303,0306). Right now all the values of the "Code" column are the character.

a <- data.frame(Code = c("AE", "0303","0306","0701","BA","UAE","6120"))

Upvotes: 1

Views: 90

Answers (4)

Peter
Peter

Reputation: 12699

There will be lots of ways to do this. In base R you could do this:

a <- data.frame(Code= c("AE", "0303", "0306", "0701", "BA", "UAE", "6120"))

a$Country_Code <- gsub("[^A-Z]+", NA, a$Code)
a$Product_Code <- as.numeric(gsub("[A-Z]+", NA, a$Code))

Upvotes: 2

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21400

This is a rather simple dplyr solution:

a %>% mutate(Country_code = str_extract(Code, "[A-Z]+"),
             Product_code = str_extract(Code, "\\d+"))
  Code Country_code Product_code
1   AE           AE         <NA>
2 0303         <NA>         0303
3 0306         <NA>         0306
4 0701         <NA>         0701
5   BA           BA         <NA>
6  UAE          UAE         <NA>
7 6120         <NA>         6120

Upvotes: 1

hello_friend
hello_friend

Reputation: 5788

Base R solution:

within(a, {
  country_code <- as.integer(gsub("[a-zA-Z]+", NA_character_, Code))
  product_code <- gsub("\\d+", NA_character_, Code)
  rm(Code)})

Upvotes: 2

Dr. Flow
Dr. Flow

Reputation: 486

This will separate the column

library(tidyverse)

a<-a %>%  mutate(Product_Code = str_extract(Code,   "[a-zA-Z]*"))

a<-a %>%  mutate(Country_Code = str_extract(Code,   "[0-9]*"))

a$Country_Code<-as.numeric(a$Country_Code)

Upvotes: 3

Related Questions