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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:12:19+00:00 2026-06-07T14:12:19+00:00

I have a data.frame in R. I want to create three different data.frame using

  • 0

I have a data.frame in R. I want to create three different data.frame using a loop and subset function. I have a large data.frame, but what I need is in the example below:

sex<-c("M","M","M","F","M","F")
age<-c(20,18,17,20,18,17)
name<-c("John", "Joseph", "Bill", "Sarah", "Robert", "Dana")
data<-data.frame(sex, age, name)

>data
        sex  age   name
    1   M    20    John
    2   M    18    Joseph
    3   M    17    Bill
    4   F    20    Sarah
    5   M    18    Robert
    6   F    17    Dana

What I want is:

>age17
      sex   age   name
    1 M     17    Bill
    2 F     17    Dana

>age18
      sex   age   name
    1 M     18    Joseph
    2 M     18    Robert


>age20
      sex   age   name
    1 M     20    John
    2 F     20    Sarah

I can use the commands below:

>age17<-subset(data, data[,2]==17)
>age18<-subset(data, data[,2]==18)
>age20<-subset(data, data[,2]==20)

But it is possible to use a loop to reduce the size of commands?

  • 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-07T14:12:21+00:00Added an answer on June 7, 2026 at 2:12 pm

    Use split:

    x <- split(data, data$age)
    
    x
    $`17`
      sex age name
    3   M  17 Bill
    6   F  17 Dana
    
    $`18`
      sex age   name
    2   M  18 Joseph
    5   M  18 Robert
    
    $`20`
      sex age  name
    1   M  20  John
    4   F  20 Sarah
    

    You now have a list of data frames, and you can access the elements of the list using any of the extract operators, i.e. $, [ or [[.

    For example, the first element:

    x[1]
    $`17`
      sex age name
    3   M  17 Bill
    6   F  17 Dana
    

    Or the element with name 17. But notice that you have to use backticks to refer to these names, since names starting with numerals are not standard:

    x$`17`
      sex age name
    3   M  17 Bill
    6   F  17 Dana
    
    • 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 with several variables. What I want is create a
I have a data frame with several rows and I want to create a
I have a data frame and I want to learn how the summary generates
I have a matrix A which I want to convert into a data.frame of
I have the following data: a=c(1:10) b=c(16:25) c=c(24:33) wa=c(3,7,3,3,3,3,3,3,3,1) wb=c(3,2,3,3,3,3,3,3,3,8) wc=c(4,1,4,4,4,4,4,4,4,1) z=data.frame(a,b,c,wa,wb,wc) I want
Imagine you have a file sink(example.txt) data.frame(a = runif(10), b = runif(10), c =
I have a data frame with a quantitative variable, x, and several different factors,
I have aggregated data using pandas data frame. Below is some actual data shown
I have a state column in a dataframe and I want to create two
I have the following dummy code: dt<-data.frame(country=letters[1:20],val=rnorm(20),siz=rnorm(20)) qplot(x=country,y=val,data=dt,geom=point,size=siz) Now I want to increase the

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.