Adam Smith
Adam Smith

Reputation: 141

R Split String By Delimiter in a column

I have a file with several lines. For example

A               B       C    
awer.ttp.net    Code    554
abcd.ttp.net    Code    747
asdf.ttp.net    Part    554
xyz.ttp.net     Part    747

I want to use R to split just column A of the table and I want a new column added to the table D, with values awe, abcd, asdf, and xyz. Would prefer this to be done using dplyr.

Upvotes: 11

Views: 23646

Answers (3)

Hamed Yazarloo
Hamed Yazarloo

Reputation: 59

First I create the dataframe:

library(dplyr)

data <- 
   data.frame(
        A=c("awer.ttp.net","abcd.ttp.net", "asdf.ttp.net", "xyz.ttp.net"),
        B=c("Code", "Code", "Part", "Part"),
        C=c(554,747,554,747), 
        stringsAsFactors = F
   )

Then mutate the new column this way:

data %>% 
    mutate(
        D = strsplit(A, "[.]") %>% 
            as.data.frame() %>% 
            t %>% 
            data.frame(stringsAsFactors = F) %>% 
            pull(1)
   )

Upvotes: 1

Lamia
Lamia

Reputation: 3875

You can use mutate and gsub:

library(dplyr)
df = df %>% mutate(D=gsub("\\..*","",A))

              A    B   C    D
   awer.ttp.net Code 554 awer
   abcd.ttp.net Code 747 abcd
   asdf.ttp.net Part 554 asdf
    xyz.ttp.net Part 747  xyz

Upvotes: 12

akrun
akrun

Reputation: 886998

We can use tidyverse for this

library(dplyr)
library(tidyr)
df1 %>% 
   separate(A, into = 'D', extra = 'drop', remove = FALSE) %>% 
   select(LETTERS[1:4])
#             A    B   C    D
#1 awer.ttp.net Code 554 awer
#2 abcd.ttp.net Code 747 abcd
#3 asdf.ttp.net Part 554 asdf
#4  xyz.ttp.net Part 747  xyz

Upvotes: 11

Related Questions