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 566165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:56:55+00:00 2026-05-13T12:56:55+00:00

I have a list containing 98 items. But each item contains 0, 1, 2,

  • 0

I have a list containing 98 items. But each item contains 0, 1, 2, 3, 4 or 5 character strings.

I know how to get the length of the list and in fact someone has asked the question before and got voted down for presumably asking such an easy question.

But I want a vector that is 98 elements long with each element being an integer from 0 to 5 telling me how many character strings there are in each list item.
I was expecting the following to work but it did not.

lapply(name.of.list,length())

From my question you will see that I do not really know the nomeclature of lists and items. Feel free to straighten me out.

  • 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-13T12:56:56+00:00Added an answer on May 13, 2026 at 12:56 pm

    Farrel, I do not exactly follow as ‘item’ is not an R type. Maybe you have a list of length 98 where each element is a vector of character string?

    In that case, consider this:

    R> fl <- list(A=c("un", "deux"), B=c("one"), C=c("eins", "zwei", "drei"))
    R> lapply(fl, function(x) length(x))
    $A
    [1] 2
    
    $B
    [1] 1
    
    $C
    [1] 3
    R> do.call(rbind, lapply(fl, function(x) length(x)))
      [,1]
    A    2
    B    1
    C    3
    R> 
    

    So there is you vector of the length of your list, telling you how many strings each list element has. Note the last do.call(rbind, someList) as we got a list back from lapply.

    If, on the other hand, you want to count the length of all the strings at each list position, replace the simple length(x) with a new function counting the characters:

    R> lapply(fl, function(x) { sapply(x, function(y) nchar(y)) } )
    $A
      un deux 
       2    4 
    
    $B
    one 
      3 
    
    $C
    eins zwei drei 
       4    4    4 
    
    R> 
    

    If that is not want you want, maybe you could mock up some example input data?

    Edit:: In response to your comments, what you wanted is probably:

    R> do.call(rbind, lapply(fl, length))
      [,1]
    A    2
    B    1
    C    3
    R> 
    

    Note that I pass in length, the name of a function, and not length(), the (displayed) body of a function. Because that is easy to mix up, I simply apply almost always wrap an anonymous function around as in my first answer.

    And yes, this can also be done with just sapply or even some of the **ply functions:

    R> sapply(fl, length)
    A B C 
    2 1 3 
    R> lapply(fl, length)
    [1] 2 1 3
    R> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of items, where each item is a simple class containing
I have a list of content items, each with a set width but different
I have a list of items (containing integer ids). I need to delete all
I have a list containing version strings, such as things: versions_list = [1.1.2, 1.0.0,
Say I have a list containing strings that look like this: 00x000s00g00wfds0d dkdf00sdf00sdfg00jk kf00dfjkd0sdl0sd0f0
I have a list of type A, in which each element contains another list
I have an R list containing a series of tables. Each table in the
I have a listview with each item containing an image, a textview and a
I have several objects of type Item. The items are ordered but their ordering
I'm completely puzzled here. I have a list of objects each containing a location.

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.