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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:15:02+00:00 2026-05-24T08:15:02+00:00

When subsetting a data.frame inside of a list, I get vectors instead of a

  • 0

When subsetting a data.frame inside of a list, I get vectors instead of a data.frames (see the example below). How to avoid this and get a data.frames?

l <- list(data.frame(a=c(1,2,3)), data.frame(b=c(4,5,6,5)), data.frame(c=c(3,4,5,6)))
names(l) <- c("A", "B", "C")
l
lapply(l, function(x) x[2:nrow(x), ])

output

> l <- list(data.frame(a=c(1,2,3)), data.frame(b=c(4,5,6,5)), data.frame(c=c(3,4,5,6)))
> names(l) <- c("A", "B", "C")
> l
$A
  a
1 1
2 2
3 3

$B
  b
1 4
2 5
3 6
4 5

$C
  c
1 3
2 4
3 5
4 6

> lapply(l, function(x) x[2:nrow(x), ])
$A
[1] 2 3

$B
[1] 5 6 5

$C
[1] 4 5 6
  • 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-24T08:15:02+00:00Added an answer on May 24, 2026 at 8:15 am

    You need the ,drop=FALSE argument

    > res <- lapply(l, function(x) x[2:nrow(x),, drop=FALSE])
    > sapply(res,class)
               A            B            C 
    "data.frame" "data.frame" "data.frame" 
    > res
    $A
      a
    2 2
    3 3
    
    $B
      b
    2 5
    3 6
    4 5
    
    $C
      c
    2 4
    3 5
    4 6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame like this: (t=structure(list(count = c(NA, 2, NA, NA, NA, 8,
When subsetting a data.frame or vector, is the same subset call guaranteed to return
Is there a way to select a subset from objects (data frames, matrices, vectors)
I am having difficulty subsetting my data by factors in a for loop. Here
This actually corresponds to my other question but things get more complicated. I have
Sample datas : > mat1 = as.data.frame(matrix(c(D-J10-N1,D-J10-N2,D-J2-N1,D-J2-N2,3,6,5,7,8,4,2,3,4,1,2,3), ncol = 4)); > mat1 V1 V2
Suppose I have the following dataframe: df <- data.frame(BR.a=rnorm(10), BR.b=rnorm(10), BR.c=rnorm(10), USA.a=rnorm(10), USA.b =
This post extends the error while submitting data in the form django model.py from
I've finally decided to put the sort.data.frame method that's floating around the internet into
#It is possible! Read below. First of all, let me use this diagram to

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.