kathystehl
kathystehl

Reputation: 841

R: Extract first two digits from nested list elements

For the following vector, I would to keep only the first two digits of each integer:

a <- c('1234 2345 345 234', '323 55432 443', '43 23345 321')

I've attempted to do this by converting the vector into a nested list using strsplit and then applying substr to the list:

a <- strsplit(a, ' ')
a <- substr(a, start = 1, stop = 2)

However, this seems to just extract eh beginning of the concatenated command:

a
[1] "c(" "c(" "c("

Ideally, I would be able to coerce the vector into the following form:

[[1]]
[1] "12" "23" "34" "23"

[[2]]
[1] "32" "55" "44"

[[3]]
[1] "43" "23" "32"

Upvotes: 1

Views: 253

Answers (2)

akrun
akrun

Reputation: 887148

We can also extract the first two digits from a word boundary

library(stringr)
str_extract_all(a, "\\b\\d{2}")
#[[1]]
#[1] "12" "23" "34" "23"

#[[2]]
#[1] "32" "55" "44"

#[[3]]
#[1] "43" "23" "32"

Upvotes: 0

MrFlick
MrFlick

Reputation: 206242

How about

lapply(strsplit(a, " "), substr, 1, 2)

this explicitly does an lapply over the results of the strsplit. This is because substr() tries to coerce your list to a character vector first (it doesn't expect a list as it's first parameter). You can see what it's looking at if you do

as.character(strsplit(a, ' '))
# [1] "c(\"1234\", \"2345\", \"345\", \"234\")" "c(\"323\", \"55432\", \"443\")"         
# [3] "c(\"43\", \"23345\", \"321\")"  

Upvotes: 3

Related Questions