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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:13:05+00:00 2026-05-31T20:13:05+00:00

This is a follow-up question related to my previous post . Below is a

  • 0

This is a follow-up question related to my previous post. Below is a more explanatory version of “what I want to do” as opposed to “how do I make this method work”.

Below is code that produces a “master” database, from which, I extract elements for further use in other functions. I routinely extract elements of data based on the value of a group identification number.

  • Objective: I would like to be able to “wrap” the specifications that vary (like the name of the output dataframe and the groups selected) into a function that could be called.

#####  generating data for example

set.seed(271828)

n.elements <- c(10,10,12,14,16,18)
group.number <- rep(1001:1006, n.elements)
element.id <- c(
    seq(1,n.elements[1], 1),
    seq(1,n.elements[2], 1),
    seq(1,n.elements[3], 1), 
    seq(1,n.elements[4], 1),
    seq(1,n.elements[5], 1),
    seq(1,n.elements[6], 1) ) 

x1 <- round(rnorm(length(group.number),45, 12), digits=0)
x2 <- round(rbeta(length(group.number),2,4), digits = 2)

data.base <- as.data.frame(cbind(group.number, element.id, x1, x2))
data.base

#####  data.base is representative of the large database 


#####  suppose I need to pull a set together made up of groups: 
#####  1003, 1004, and 1001 


groups.set.1 <- as.data.frame(c(1003, 1004, 1001))
bank.names <- c("group.number")
colnames(groups.set.1) <- bank.names
set.sort <- matrix(seq(1,nrow(groups.set.1),1)) 
sort.set.1 <- cbind(groups.set.1, set.sort)

set.1 <- as.data.frame(merge(sort.set.1, data.base, 
by="group.number", all.x=TRUE))

#####  this is how the dataset needs to be ordered for further use
set.1 <- set.1[order(set.1$set.sort, set.1$element.id ), ]
row.names(set.1) <- seq(nrow(set.1))

EDIT: Suppose I wanted to carry out the same task to produce set.2, where set.2 is made up of groups: 1005, 1006, and 1002. I could just copy the above code, and make the relevant changes. However, I would like to know if it is possible to specify a function so that I can pass the necessary changes to it, and have it produce the output dataframe as desired. Perhaps having a function called group.extract, where I could specify something like the following:

groups.2 <- c(1005, 1006, 1002)
group.extract(set.2, groups.2)

Based on the comments provided, it seems like a list is the way to go, and have the function call the list, where the list elements can vary.

  • 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-31T20:13:07+00:00Added an answer on May 31, 2026 at 8:13 pm

    I’d write this function using match, as follows. Here I’ve hard-coded the names of the columns of the input data frame to use for matching and sorting; those could also be added as optional inputs. The column order of the output is slightly different from yours but that could be easily changed as well.

    getset <- function(g, d=data.base) {
      d$set.sort <- match(d$group.number, g)
      d <- d[!is.na(d$set.sort),]
      d <- d[order(d$set.sort, d$element.id),]
      rownames(d) <- NULL
      d
    }
    

    You’d use it almost exactly like you propose:

    > set.1 <- getset(c(1003, 1004, 1001))
    > head(set.1)
      group.number element.id x1   x2 set.sort
    1         1003          1 60 0.32        1
    2         1003          2 28 0.18        1
    3         1003          3 42 0.47        1
    4         1003          4 43 0.08        1
    5         1003          5 45 0.31        1
    6         1003          6 27 0.48        1
    

    Though if you have multiple groups to get, putting them in a list and using lapply would be the way to go.

    > groups <- list(group1=c(1003, 1004, 1001), group2=c(1005,1006,1002))
    > sets <- lapply(groups, getset)
    > lapply(sets, head)
    $group1
      group.number element.id x1   x2 set.sort
    1         1003          1 60 0.32        1
    2         1003          2 28 0.18        1
    3         1003          3 42 0.47        1
    4         1003          4 43 0.08        1
    5         1003          5 45 0.31        1
    6         1003          6 27 0.48        1
    
    $group2
      group.number element.id x1   x2 set.sort
    1         1005          1 27 0.20        1
    2         1005          2 51 0.48        1
    3         1005          3 49 0.43        1
    4         1005          4 48 0.20        1
    5         1005          5 33 0.37        1
    6         1005          6 41 0.50        1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This post is a follow-up of a related question posted here by Ran .
This is a follow up question to a previous question I asked about calculating
This is really a follow on question to a previous one , where I
This question is rather agnostic than related to a certain version control program. Assume
This is a follow up question related to how to find out the scaling
This is a related question , some sort of a follow up. Let's say
This question is related to BuddyPress, and a follow-up question from this question I
This is a follow up to my related question posted previously. My .NET application
This is related to my previous question . An unforeseen issue arose with the
I have a question related to this previous question of mine . In an

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.