Need the first and the third element after doing strsplit
2
0
Entering edit mode
7.2 years ago
horsedog ▴ 60

I have a quick question about to extract the the first and the third element after doing the strsplit in R, here is the example:

Bacteroides oleiciplenus YIT 12058

what I need is Bacteroides YIT What I can do with it? I only know how to extract the first one using "head,1" Thank you very much

R • 10k views
ADD COMMENT
4
Entering edit mode
7.2 years ago
> library(stringr)
> str_c(str_split_fixed("Bacteroides oleiciplenus YIT 12058", " ",4)[,c(1,3)],collapse=" ")
[1] "Bacteroides YIT"

Bash solution:

 $ echo "Bacteroides oleiciplenus YIT 12058" | cut -f1,3 -d" " 
 Bacteroides YIT

Awk solution:

$  echo "Bacteroides oleiciplenus YIT 12058" | awk  '{print $1,$3}' 
  Bacteroides YIT
ADD COMMENT
2
Entering edit mode
7.2 years ago

Not really a bioinformatics question. It's an R programming one. Anyway, since I'm here:
As per the doc, strsplit returns a list so:

lst <- strsplit(mystring, " ")  # split string on space
lst[[1]][2] # access second element
ADD COMMENT

Login before adding your answer.

Traffic: 2789 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6