Reputation: 201
I have a column (geneDesc) in a data frame (bacteria) that I want to split into two columns. The column contains the gene ID and the species name of the organism the gene comes from in brackets.
For example:
geneDesc
hypothetical protein, partial [Vibrio shilonii]
ankyrin repeat protein [Leptospira kirschneri]
helicase [Alteromonas macleodii]
I'm using the following command:
bacteria2 <- separate(bacteria, geneDesc, c("gene", "species"), sep = "\\[")
But I get this error:
Error: Values not split into 2 pieces at 341, 342, 448, 450, etc...
Is there a way to run the command anyway and just create another column where there is another "["? Everything after the first bracket is of no interest.
Upvotes: 1
Views: 278
Reputation: 2226
You almost have it but your sep
regular expression needs adjusted to match either a [
or ]
:
library(tidyr)
bacteria %>% separate(geneDesc,c("gene","species"), sep="[\\[\\]]", extra="drop")
Output:
gene species
1 hypothetical protein, partial Vibrio shilonii
2 ankyrin repeat protein Leptospira kirschneri
3 helicase Alteromonas macleodii
Upvotes: 1
Reputation: 531
If you only want to remove everything after the first bracket I suggest gsub
> df <- read.table(text='hypothetical protein, partial [Vibrio shilonii]
+ ankyrin repeat protein [Leptospira kirschneri]
+ helicase [Alteromonas macleodii]', sep='\n')
> df
V1
1 hypothetical protein, partial [Vibrio shilonii]
2 ankyrin repeat protein [Leptospira kirschneri]
3 helicase [Alteromonas macleodii]
> gsub('\\s+\\[.*$', '', df$V1)
[1] "hypothetical protein, partial" "ankyrin repeat protein" "helicase"
> data.frame(data=gsub('\\s+\\[.*$', '', df$V1))
data
1 hypothetical protein, partial
2 ankyrin repeat protein
3 helicase
Upvotes: 0
Reputation: 4024
separate(..., extra = "drop")
or
separate(..., extra = "merge")
another option is
library(stringr)
library(dplyr)
bacteria %>%
mutate(gene = geneDesc %>% str_replace_all(" *\\[.*$", "") )
Upvotes: 0