Louis GRIMALDI
Louis GRIMALDI

Reputation: 101

Extracting every nth element of vector of lists

I have the following ids.

ids <- c('a-000', 'b-001', 'c-002')

I want to extract the numeric part of them (001, 002, 003). I tried this :

(str_split(ids, '-', n=2))[2]

returns the following :

[[1]]
[1] "b"   "001"

I don't want the second element of the list. I want the second element of all elements in the vector. I know this is definitely a basic question, but how do I resolve the syntax conflict? Going through lambda function ?

Upvotes: 0

Views: 476

Answers (5)

akrun
akrun

Reputation: 886998

An option with str_replace

library(stringr)
str_replace(ids, "\\D+", "")
#[1] "000" "001" "002"

Upvotes: 0

user13653858
user13653858

Reputation:

With base R you can remove all the characters that are not digits:

ids <- c('a-000', 'b-001', 'c-002')

gsub("[^[:digit:]]", "", ids)
#> [1] "000" "001" "002"

[:digit:] is regex for digit and ^ means everything that is not a digit, so you basically replace every other characters with empty string "".

For more information see documentation for gsub() and regex in R.

Upvotes: 1

jay.sf
jay.sf

Reputation: 72653

The function is also available in base R.

sapply(strsplit(ids, "-"), `[`, 2)
# [1] "000" "001" "002"

You can also try gsub and substring.

gsub("\\D+", "", ids)
# [1] "000" "001" "002"
substring(ids, 3)
# [1] "000" "001" "002"

Upvotes: 2

zhao
zhao

Reputation: 91

You could try str_remove(ids, "\\D+")

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388862

To continue with your attempt, you can use sapply :

sapply(stringr::str_split(ids, '-', n=2), `[`, 2)
#[1] "000" "001" "002"

It is better to use str_split_fixed though here.

stringr::str_split_fixed(ids, '-', n=2)[, 2]
#[1] "000" "001" "002"

Or in base R :

sub('.*?-(.*)-?.*', '\\1', ids)

Upvotes: 1

Related Questions