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):

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 changeDIMin the function below.)Here are the groups on which we will split
myd. In this example, we are splittingmydfirst 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).This is the splitting process. We first split up just by names, and match those names with
mydto create a list ofdata.frames.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.
Here’s a sample of the output: