Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择n个后续分组变量并在r中应用函数_R_Variables_Loops - Fatal编程技术网

选择n个后续分组变量并在r中应用函数

选择n个后续分组变量并在r中应用函数,r,variables,loops,R,Variables,Loops,以下是示例数据: 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 myd我建议重新编码myfun以获取

以下是示例数据:

 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

myd我建议重新编码myfun以获取矩阵并使用plotrix软件包中的Pastecol

library(plotrix)

myfun = function(x){
    out = pasteCols(t(x), sep = ":")
    # some code
    return(out)
}  
然后,非常简单:对于每个组,使用模和整数除法计算调用myfun时要使用的第一列和最后一列的索引:

rubiques_solution = function(group, myd, num_to_group){
   # loop over groups
   for(g in unique(group)){
      var_index = which(group == g)
      num_var = length(var_index)

      # test to make sure num_to_group is smaller than the number of variable
      if(num_var < num_to_group){
         stop("num_to_group > number of variable in at least one group")
         }

      # number of calls to myfun
      num_calls = num_var %/% num_to_group

      # the idea here is that we create the first and last column
      # in which we are interested for each call
      first = seq(from = var_index[1], by = num_to_group, length = num_calls)
      last = first + num_to_group -1
      # the last call will contain possibly more varialbe, we adjust here:
      last[length(last)] = last[length(last)] + (num_var %% num_to_group)

      for(i in num_calls){
         # maybe do something with the return value of myfun ?
         myfun(myd[,first[i]:last[i]])
      }
   }  
}  

group = rep(1:10, each = 10) # same than yours
myd = data.frame (matrix (sample (c("AB", "BB", "AA"), 100*100, replace = T), ncol = 100)) # same than yours
num_to_group = 2 # this is your first example
rubiques_solution(group, myd, num_to_group)
rubiques\u solution=函数(组、myd、num\u到组){
#循环组
对于(唯一(组)中的g){
var_指数=哪个(组==g)
num\u var=长度(var\u索引)
#测试以确保num_to_group小于变量的数量
if(数值变量<数值到数值组){
停止(“num_to_group>至少一个组中的变量数”)
}
#呼叫myfun的次数
num\u calls=num\u var%/%num\u到组
#这里的想法是我们创建第一列和最后一列
#我们对每个电话都感兴趣
first=seq(from=var\u index[1],by=num\u to\u group,length=num\u调用)
最后=第一个+第二组的数量-1
#最后一个调用可能包含更多变量,我们在这里进行调整:
上次[长度(上次)]=上次[长度(上次)]+(数值变量%%num数值到数值组)
for(i在num_调用中){
#也许用myfun的返回值做点什么?
myfun(myd[,first[i]:last[i]])
}
}  
}  
组=代表(1:10,每个=10)#与您的相同
myd=data.frame(矩阵(样本(c(“AB”、“BB”、“AA”),100*100,替换=T,ncol=100))#与您的相同
num_to_group=2#这是您的第一个示例
rubiques_解决方案(组、myd、num_到组)

希望我正确理解了这个问题。

创建一个函数,将您的数据分割成适当的列表,并对列表应用您想要的任何函数

此函数将创建第二个分组变量。(问题中提供了第一个分组变量(
group
),如果更改该值,还应在下面的函数中更改
DIM

以下是我们将拆分的组
myd
。在本例中,我们首先将
myd
拆分为10个列组,然后将每组拆分为3个列组,最后一个组除外,该组将有4个列(3+3+4=10)

注意:要更改分组依据的列数,例如,一次按两个变量分组,请将
group2=rep(myfun(3),length.out=100)
更改为
group2=rep(myfun(2),length.out=100)

现在我们有了清单,我们可以做很多有趣的事情。您想将列粘贴在一起,并用冒号分隔。下面是你怎么做到的

# 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
以下是输出的示例:

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"
lappy(最终,功能(x)头(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”
# 
# >
#
#$MR.1.4_MR.1.5_MR.1.6
#[1]“AB:BB:AA”“BB:BB:BB”“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”
# 
# >
#
#$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:AA”
# 
#$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:BB”
# 
#$MR.4.37_MR.4.38_MR.4.39_MR.4.40
#[1]“BB:BB:AB:AA”“AA:BB:AA:BB”“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:BB”
# 
#$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”
# 
#$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: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”“AB:AA:BB:BB”
# 
#$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: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”

虽然没有明确的答案,但只是一个想法-你可以创建变量名称Mat Rubique的名称,你能举例说明你的函数是如何应用的吗?你看不到输出,因为在你的例子中,你似乎只调用myfun,所以我就是这么做的,如果你需要像其他答案一样输入一个列表的话(或在数据帧中…相同的东西)
library(plotrix)

myfun = function(x){
    out = pasteCols(t(x), sep = ":")
    # some code
    return(out)
}  
rubiques_solution = function(group, myd, num_to_group){
   # loop over groups
   for(g in unique(group)){
      var_index = which(group == g)
      num_var = length(var_index)

      # test to make sure num_to_group is smaller than the number of variable
      if(num_var < num_to_group){
         stop("num_to_group > number of variable in at least one group")
         }

      # number of calls to myfun
      num_calls = num_var %/% num_to_group

      # the idea here is that we create the first and last column
      # in which we are interested for each call
      first = seq(from = var_index[1], by = num_to_group, length = num_calls)
      last = first + num_to_group -1
      # the last call will contain possibly more varialbe, we adjust here:
      last[length(last)] = last[length(last)] + (num_var %% num_to_group)

      for(i in num_calls){
         # maybe do something with the return value of myfun ?
         myfun(myd[,first[i]:last[i]])
      }
   }  
}  

group = rep(1:10, each = 10) # same than yours
myd = data.frame (matrix (sample (c("AB", "BB", "AA"), 100*100, replace = T), ncol = 100)) # same than yours
num_to_group = 2 # this is your first example
rubiques_solution(group, myd, num_to_group)
myfun = function(LENGTH, DIM = 10) {
  PATTERN = rep(1:(DIM %/% LENGTH), each=LENGTH)
  c(PATTERN, rep(max(PATTERN), DIM %% LENGTH))
}
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)
# 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="_"))
# 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
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"