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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:08:54+00:00 2026-06-09T04:08:54+00:00

Here is example data: myd <- data.frame (matrix (sample (c(AB, BB, AA), 100*100, replace

  • 0

Here is example data:

 myd <- data.frame (matrix (sample (c("AB", "BB", "AA"), 100*100, 
 replace = T), ncol = 100))
 variablenames= paste (rep (paste ("MR.", 1:10,sep = ""), 
  each = 10), 1:100, sep = ".")
  names(myd) <- variablenames

Each variable has a group, here we have ten groups. Thus the group index for the each variable in this data frame is as follows:

group <- rep(1:10, each = 10)

Thus Variable names and group

 data.frame (group, variablenames)
    group variablenames
1       1        MR.1.1
2       1        MR.1.2
3       1        MR.1.3
4       1        MR.1.4
5       1        MR.1.5
6       1        MR.1.6
7       1        MR.1.7
8       1        MR.1.8
9       1        MR.1.9
10      1       MR.1.10
11      2       MR.2.11
 <<<<<<<<<<<<<<<<<<<<<<<<
100    10     MR.10.100

Each groups means that the following steps whould be applied to group of variables seperately.

I have longer function to work the following is short example:

function considering two variables at time

myfun <- function (x1, x2) {
out <- NULL
out <-  paste(x1, x2, sep=":")
# for other steps to be performed here
return (out)
}
# group 1
myfun (myd[,1], myd[,2]); myfun (myd[,3], myd[,4]); myfun (myd[,5], myd[,6]); 
myfun (myd[,7], myd[,8]); myfun (myd[,9], myd[,10]);
# group 2 
 myfun (myd[,11], myd[,12]); myfun (myd[,13], myd[,14]); .......so on to group 10 ;

In this way I need to walk for variables 1:10 (i.e. in first group to perform the above action), then 11:20 (the second group). The group doesnot matter in this case number of variables in each group are divisible with number of variables (10) taken (considered) at a time (2).

However in the following example where 3 variables taken at a time – number of total variable in each group (3), 10/3, you have one variable left over at the end.

function considering three variable at time.

myfun <- function (x1, x2, x3) {
out <- NULL
out <-  paste(x1, x2, x3, sep=":")
# for other steps to be performed here
return (out)
}
# for group 1
myfun (myd[,1], myd[,2], myd[,3])
myfun (myd[,4], myd[,5], myd[,6])
myfun (myd[,7], myd[,8], myd[,9])  
 # As there one variable left before proceedomg to second group, the final group will 
have 1 extra variable  
myfun (myd[,7], myd[,8], myd[,9],myd[,10] )
 # for group 2   
  myfun (myd[,11], myd[,12], myd[,13])
  # and to the end all groups and to end of the file.

I want to loop this process by user defined n number of variables consered at time, where n may be 1 to maximum number of variables in each group.

Edit: Just illustration to show the process (just group 1 and 2 demostrated for example):

enter image description here

  • 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-09T04:08:55+00:00Added an answer on June 9, 2026 at 4:08 am

    Create a function that will split your data up into appropriate lists, and apply whatever functions you want to your list.

    This function will create your second grouping variable. (The first grouping variable (group) is provided in your question; if you change that value, you should also change DIM in the function below.)

    myfun = function(LENGTH, DIM = 10) {
      PATTERN = rep(1:(DIM %/% LENGTH), each=LENGTH)
      c(PATTERN, rep(max(PATTERN), DIM %% LENGTH))
    }
    

    Here are the groups on which we will split myd. In this example, we are splitting myd first into 10-column groups, and each group into 3-column groups, except for the last group, which will have 4 columns (3+3+4 = 10).

    NOTE: To change the number of columns you’re grouping by, for example, grouping by two variables at a time, change group2 = rep(myfun(3), length.out=100) to group2 = rep(myfun(2), length.out=100).

    group <- rep(1:10, each = 10)
    # CHANGE THE FOLLOWING LINE ACCORDING
    # TO THE NUMBER OF GROUPS THAT YOU WANT
    group2 = rep(myfun(3), length.out=100)
    

    This is the splitting process. We first split up just by names, and match those names with myd to create a list of data.frames.

    # Extract group names for matching purposes
    temp = split(names(myd), list(group, group2))
    
    # Match the names to myd
    temp = lapply(1:length(temp),
                  function(x) myd[, which(names(myd) %in% temp[[x]])])
    
    # Extract the names from the list for future reference
    NAMES = lapply(temp, function(x) paste(names(x), collapse="_"))
    

    Now that we have a list, we can do lots of fun things. You wanted to paste your columns together separated by a colon. Here’s how you’d do that.

    # Do what you want with the list
    # For example, to paste the columns together:
    FINAL = lapply(temp, function(x) apply(x, 1, paste, collapse=":"))
    names(FINAL) = NAMES
    

    Here’s a sample of the output:

    lapply(FINAL, function(x) head(x, 5))
    # $MR.1.1_MR.1.2_MR.1.3
    # [1] "AA:AB:AB" "AB:BB:AA" "BB:AB:AA" "BB:AA:AB" "AA:AA:AA"
    # 
    # $MR.2.11_MR.2.12_MR.2.13
    # [1] "BB:AA:AB" "BB:AB:BB" "BB:AA:AA" "AB:BB:AA" "BB:BB:AA"
    # 
    # $MR.3.21_MR.3.22_MR.3.23
    # [1] "AA:AB:BB" "BB:AA:AA" "AA:AB:BB" "AB:AA:AA" "AB:BB:BB"
    # 
    # <<<<<<<------SNIP------>>>>>>>>
    #
    # $MR.1.4_MR.1.5_MR.1.6
    # [1] "AB:BB:AA" "BB:BB:BB" "AA:AA:AA" "BB:BB:AB" "AB:AA:AA"
    # 
    # $MR.2.14_MR.2.15_MR.2.16
    # [1] "AA:BB:AB" "BB:BB:BB" "BB:BB:AB" "AA:BB:AB" "BB:BB:BB"
    # 
    # $MR.3.24_MR.3.25_MR.3.26
    # [1] "AA:AB:BB" "BB:AA:BB" "BB:AB:BB" "AA:AB:AA" "AB:AA:AA"
    # 
    # <<<<<<<------SNIP------>>>>>>>>
    #
    # $MR.1.7_MR.1.8_MR.1.9_MR.1.10
    # [1] "AB:AB:AA:AB" "AB:AA:BB:AA" "BB:BB:AA:AA" "AB:BB:AB:AA" "AB:BB:AB:BB"
    # 
    # $MR.2.17_MR.2.18_MR.2.19_MR.2.20
    # [1] "AB:AB:BB:BB" "AB:AB:BB:BB" "AB:AA:BB:BB" "AA:AA:AB:AA" "AB:AB:AB:AB"
    # 
    # $MR.3.27_MR.3.28_MR.3.29_MR.3.30
    # [1] "BB:BB:AB:BB" "BB:BB:AA:AA" "AA:BB:AB:AA" "AA:BB:AB:AA" "AA:AB:AA:BB"
    # 
    # $MR.4.37_MR.4.38_MR.4.39_MR.4.40
    # [1] "BB:BB:AB:AA" "AA:BB:AA:BB" "AA:AA:AA:AB" "AB:AA:BB:AB" "BB:BB:BB:BB"
    # 
    # $MR.5.47_MR.5.48_MR.5.49_MR.5.50
    # [1] "AB:AA:AA:AB" "AB:AA:BB:AA" "AB:BB:AA:AA" "AB:BB:BB:BB" "BB:AA:AB:AA"
    # 
    # $MR.6.57_MR.6.58_MR.6.59_MR.6.60
    # [1] "BB:BB:AB:AA" "BB:AB:BB:AA" "AA:AB:AB:BB" "BB:AB:AA:AB" "AB:AA:AB:BB"
    # 
    # $MR.7.67_MR.7.68_MR.7.69_MR.7.70
    # [1] "BB:AB:BB:AA" "BB:AB:BB:AA" "BB:AB:BB:AB" "AB:AA:AA:AA" "AA:AA:AA:AB"
    # 
    # $MR.8.77_MR.8.78_MR.8.79_MR.8.80
    # [1] "AA:AB:AA:AB" "AB:AA:AB:BB" "BB:BB:AA:AB" "AB:BB:BB:BB" "AB:AA:BB:AB"
    # 
    # $MR.9.87_MR.9.88_MR.9.89_MR.9.90
    # [1] "AA:BB:AB:AA" "AA:AB:BB:BB" "AA:BB:AA:BB" "AB:AB:AA:BB" "AB:AA:AB:BB"
    # 
    # $MR.10.97_MR.10.98_MR.10.99_MR.10.100
    # [1] "AB:AA:BB:AB" "AB:AA:AB:BB" "BB:AB:AA:AA" "BB:BB:AA:AA" "AB:AB:BB:AB"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is some example data: data = data.frame(series = c(1a, 1b, 1e), reading =
Here is an example data set: data <- data.frame (author = c('bob', 'john', 'james'),
Here's a problem I'm encountering: Example Data df <- data.frame(1,2,3,4,5,6,7,8) df <- rbind(df,df,df,df) What
I have a outsource data from : http://example.com/data/news.json Here is the example result after
Here is an example of one of our data calls in our DAL using
Here is some example data in a mysql table a b distance 15 44
Here is my small dataset set.seed(123) X1 <- rep(1:3, each = 4) X2 <-
Ok here is some example data and the expected results I would like: 2011-06-26
here is example data: 10914_Excel Short Summary.xls F:\MassHunter\DATA\10921_PAIN\QuantResults\10921_PAIn.batch.bin 10918_Excel Short Summary.xls 10923_Excel Short Summary.xls
ex: <a><strike>example data in here</strike></a> I want everything inside the a tag, to 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.