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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:54:49+00:00 2026-06-08T17:54:49+00:00

here’s the issue I have a csv file which I read.csv(), and prints to

  • 0

here’s the issue

I have a csv file which I read.csv(), and prints to this:

   grp  b a  id d c
1 grp1  2 1 id3 3 2
2 grp1 -2 1 id1 3 2
3 grp0 -2 1 id4 3 2
4 grp0  1 1 id0 3 2
5 grp0  1 1 id2 3 2

Now I want to split this into two dataframes, one with data for grp1 and another for grp2

groups <- split(raw, raw$grp);

Which yields this:

$grp0
   grp  b a  id d c
3 grp0 -2 1 id4 3 2
4 grp0  1 1 id0 3 2
5 grp0  1 1 id2 3 2

$grp1
   grp  b a  id d c
1 grp1  2 1 id3 3 2
2 grp1 -2 1 id1 3 2

Now I just want the a,b,c,d rows from each of these lists, so I need to coerce them to dataframes to use subset(), which means I need to:

for(i in 1:length(groups))
{
      x <- subset(as.data.frame(groups[i]), select = c(a,b,c,d));
      some_function(x); 
}

Problem is, when I do this, it says column a does not exist, and when I print this stuff out, this is what we see:

 grp0.grp grp0.b grp0.a grp0.id grp0.d grp0.c
3     grp0     -2      1     id4      3      2
4     grp0      1      1     id0      3      2
5     grp0      1      1     id2      3      2
  grp1.grp grp1.b grp1.a grp1.id grp1.d grp1.c
1     grp1      2      1     id3      3      2
2     grp1     -2      1     id1      3      2

So these columns are no longer just a,b,c,d but their name prepended by the name created during the split. Is their a way I can avoid this happening? Or is there a way to get the name of the dataframe and prepend it to the list of elements I am subsetting? I just want to end up with dataframes that look something like this, the column names need not be exact:

$grp0
       b  a  d c
    3 -2  1  3 2
    4  1  1  3 2
    5  1  1  3 2

$grp1
       b  a  d c
    1  2  1  3 2
    2 -2  1  3 2
  • 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-08T17:54:50+00:00Added an answer on June 8, 2026 at 5:54 pm

    In the future, please use dput() on any object that you want to present to us, so we don’t have to manually type in your example to write the code.

    Are you a SAS programmer? You don’t need semicolons after every line…

    raw <- data.frame(
        grp=c("grp1","grp1","grp0","grp0","grp0"),
        b=c(2,-2,2,1,1),
        a=rep(1,5),
        id=c("id3","id1","id4","id0","id2"),
        d=rep(3,5),
        c=rep(2,5)
    )
    groups <- split(raw, raw$grp)
    

    You were actually pretty close.
    I’d recommend reading up on the difference between ?`[` and ?`[[`. Here’s your corrected code:

    x <- list()
    for(i in 1:length(groups)) {       
    x[[i]] <- subset(as.data.frame(groups[[i]]), select = c(a,b,c,d));     
      } 
    names(x) <- names(groups)
    x
    

    With that said, please try to learn the ?lapply functions

    lapply(groups, function(.groups) .groups[,c("a","b","c","d")])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is what I am trying to achieve in PHP: I have this string:
Here is the issue I am having: I have a large query that needs
Here's the context: I have a file (or stream), and I want to process
Here's my situation: I have a Data Template set up which contains a ToggleButton
Here is the code in a function I'm trying to revise. This example works
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
I have just tried to save a simple *.rtf file with some websites and
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();

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.