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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:14:35+00:00 2026-06-01T11:14:35+00:00

I have a list within a data frame (in this example, df$candpct ), and

  • 0

I have a list within a data frame (in this example, df$candpct), and would like to extract the first and second elements from the list and place them into separate vectors.

Here is the initial structure of the data frame:

> head(df)
  juris                         candpct
1   101              0.752491, 0.247509
2   102 0.4856056, 0.2850754, 0.1845157
3   103                               1
4   104                               1

I’ve looked at this answer, which showed me how to extract the first element effectively:

> df$first_place <- sapply(df$candpct,"[[",1)

> head(df)
      juris                         candpct first_place
    1   101              0.752491, 0.247509    0.752491
    2   102 0.4856056, 0.2850754, 0.1845157   0.4856056
    3   103                               1           1
    4   104                               1           1

But if I try and run another line to extract the second element, I get an error as not all rows have an element in the second position.

> df$second_place <- sapply(df$candpct,"[[",2)
Error in FUN(X[[3L]], ...) : subscript out of bounds

Ideally, I’d like for it to fill the corresponding row in second_place with NA or 0 if the second element is missing.

I’m sure there are various approaches for how to deal with this problem, but I don’t have a lot of experience working with lists so I’m having a lot of trouble getting started.

Thanks in advance for your help.

  • 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-06-01T11:14:37+00:00Added an answer on June 1, 2026 at 11:14 am

    Just following up on my comment:

    l <- list(a = 1,b = 1:2,c = 1:3)
    > sapply(l,'[',2)
     a  b  c 
    NA  2  2 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two lists with 8 list elements within each one. I would like
I have a large data frame (named z ) that looks like this: RPos
How can I get a data frame's name from a list? Sure, get() gets
Suppose I have a date.frame like: df <- data.frame(a=1:5, b=sample(1:5, 5, replace=TRUE), c=5:1) df
I have a list with time related data. the item renderers display this time.
I have created a custom ArrayAdapter to display data within ListView items from an
Possible Duplicate: write.csv() a list of unequally sized data.frames I have a data frame
I have a List<class> that I would like to convert into a json object
I have a list within Sharepoint, using a custom new form I have added
I have a list of images within a div tag with the id sIMG

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.