Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 861985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:04:05+00:00 2026-05-15T09:04:05+00:00

When creating functions that use strsplit , vector inputs do not behave as desired,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:04:06+00:00Added an answer on May 15, 2026 at 9:04 am

    In general, you should try to use a vectorized function to begin with. Using strsplit will frequently require some kind of iteration afterwards (which will be slower), so try to avoid it if possible. In your example, you should use nchar instead:

    > nchar(words)
    [1] 1 5 5 3
    

    More generally, take advantage of the fact that strsplit returns a list and use lapply:

    > as.numeric(lapply(strsplit(words,""), length))
    [1] 1 5 5 3
    

    Or else use an l*ply family function from plyr. For instance:

    > laply(strsplit(words,""), length)
    [1] 1 5 5 3
    

    Edit:

    In honor of Bloomsday, I decided to test the performance of these approaches using Joyce’s Ulysses:

    joyce <- readLines("http://www.gutenberg.org/files/4300/4300-8.txt")
    joyce <- unlist(strsplit(joyce, " "))
    

    Now that I have all the words, we can do our counts:

    > # original version
    > system.time(print(summary(sapply(joyce, function (x) length(strsplit(x,"")[[1]])))))
       Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      0.000   3.000   4.000   4.666   6.000  69.000 
       user  system elapsed 
       2.65    0.03    2.73 
    > # vectorized function
    > system.time(print(summary(nchar(joyce))))
       Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      0.000   3.000   4.000   4.666   6.000  69.000 
       user  system elapsed 
       0.05    0.00    0.04 
    > # with lapply
    > system.time(print(summary(as.numeric(lapply(strsplit(joyce,""), length)))))
       Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      0.000   3.000   4.000   4.666   6.000  69.000 
       user  system elapsed 
        0.8     0.0     0.8 
    > # with laply (from plyr)
    > system.time(print(summary(laply(strsplit(joyce,""), length))))
       Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      0.000   3.000   4.000   4.666   6.000  69.000 
       user  system elapsed 
      17.20    0.05   17.30
    > # with ldply (from plyr)
    > system.time(print(summary(ldply(strsplit(joyce,""), length))))
           V1        
     Min.   : 0.000  
     1st Qu.: 3.000  
     Median : 4.000  
     Mean   : 4.666  
     3rd Qu.: 6.000  
     Max.   :69.000  
       user  system elapsed 
       7.97    0.00    8.03 
    

    The vectorized function and lapply are considerably faster than the original sapply version. All solutions return the same answer (as seen by the summary output).

    Apparently the latest version of plyr is faster (this is using a slightly older version).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 449k
  • Answers 449k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The short answer is that you did not schedule the… May 15, 2026 at 8:05 pm
  • Editorial Team
    Editorial Team added an answer You can get all three by renaming a file differently… May 15, 2026 at 8:05 pm
  • Editorial Team
    Editorial Team added an answer Yes, it matters, because std::string cannot be compared with a… May 15, 2026 at 8:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.