R strsplit and vectorization

Posted by James on Stack Overflow See other posts from Stack Overflow or by James
Published on 2010-06-16T15:14:31Z Indexed on 2010/06/16 15:22 UTC
Read the original article Hit count: 546

Filed under:
|
|

When creating functions that use strsplit, vector inputs do not behave as desired, and sapply needs to be used. This is due to the list output that strsplit produces. Is there a way to vectorize the process - that is, the function produces the correct element in the list for each of the elements of the input?

For example, to count the lengths of words in a character vector:

words <- c("a","quick","brown","fox")

> length(strsplit(words,""))
[1] 4 # The number of words (length of the list)

> length(strsplit(words,"")[[1]])
[1] 1 # The length of the first word only

> sapply(words,function (x) length(strsplit(x,"")[[1]]))
a quick brown   fox 
1     5     5     3 
# Success, but potentially very slow

Ideally, something like length(strsplit(words,"")[[.]]) where . is interpreted as the being the relevant part of the input vector.

© Stack Overflow or respective owner

Related posts about r

    Related posts about vectorization