Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Split String By Delimiter in a column

Tags:

r

dplyr

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.

like image 657
Adam Smith Avatar asked Jul 08 '17 00:07

Adam Smith


People also ask

How to split a variable based on a delimiter in R?

The basic installation of R provides a solution for the splitting of variables based on a delimiter. If we want to split our variable with Base R, we can use a combination of the data.frame, do.call, rbind, strsplit, and as.character functions.

How to split one data frame column into multiple in R?

If you want to split one data frame column into multiple in R, then here is how to do that in 3 different ways. Here is the data frame that I created from the mtcars dataset. I will split this column using space as a delimiter, and to do that, I will create two calculations. The first calculation is the count of space characters in a string.

How to split a column by delimiter in Excel?

One of the best solutions to split column by delimiter is function separate from the tidyr. In the description of function separate are a couple of examples. There is a lot of parameters that can be useful to do some configurations for the desired result. By the default function separate will remove the original column.

How to split a string in R?

The strsplit () function is extensively used and most popular in terms of splitting the strings. In the R language, we use the paste () function to concatenate and the strsplit () function to split the string. Let’s see how to split the string.


Video Answer


2 Answers

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
like image 111
Lamia Avatar answered Oct 05 '22 05:10

Lamia


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
like image 23
akrun Avatar answered Oct 05 '22 04:10

akrun