Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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
R中的置换与分组_R_Combinations_Permutation - Fatal编程技术网

R中的置换与分组

R中的置换与分组,r,combinations,permutation,R,Combinations,Permutation,假设R中有三列。第一种是1:4的随机抽样,并进行替换。第二种是1:4,根据需要重复。第三个只是一个索引。结果应该输出一个顺序无关紧要的数字粘贴组合,并给出一个计数。请注意,示例中未填写n,但应能适用于所有n。如上所述的例子: c1 <- sample(1:4, n, replace = TRUE) c2 <- c(4:1) c3 <- 1 cbind(c1, c2, c3) 显示x[0:9]!=0 谢谢你的帮助!祝你好运 我相信我们已经解决了自己的问题。最初的问题涉及的是起

假设R中有三列。第一种是1:4的随机抽样,并进行替换。第二种是1:4,根据需要重复。第三个只是一个索引。结果应该输出一个顺序无关紧要的数字粘贴组合,并给出一个计数。请注意,
示例
中未填写n,但应能适用于所有n。如上所述的例子:

c1 <- sample(1:4, n, replace = TRUE)
c2 <- c(4:1)
c3 <- 1

cbind(c1, c2, c3)
显示
x[0:9]!=0


谢谢你的帮助!祝你好运

我相信我们已经解决了自己的问题。最初的问题涉及的是起点和终点,而不是数值。所以,我发布了这个答案——很容易切换到数字。希望这对将来的人有所帮助

library(dplyr)
library(sqldf)
x<-data.frame(orig=as.character(sample(LETTERS[1:10], 100, replace=TRUE)), dest=as.character(sample(LETTERS[1:10], 100, replace=TRUE)))
x$orig<-as.character(x$orig)
x$dest<-as.character(x$dest)

x1<-sort(unique(c(x[,1], x[,2])))

x_ind<-data.frame(loc=x1, ind=1:length(x1))

xx<-sqldf("select a.*, b.ind as orig_ind from x as a left join x_ind as b on a.orig=b.loc")
xxx<-sqldf("select a.*, b.ind as dest_ind from xx as a left join x_ind as b on a.dest=b.loc")

comb<-function(orig, dest, orig_ind, dest_ind)
  {
  if (orig_ind<=dest_ind)
  {
    out<-paste(orig, dest, sep="-")
  } else 
  {
    out<-paste(dest, orig, sep="-")
  }
  return(out)
}

orig_dest<-apply(xxx, 1, function(x) comb(x[1], x[2], x[3], x[4]))
xxxx<-data.frame(xxx, orig_dest)
库(dplyr)
库(sqldf)

谢谢你的编辑。
library(dplyr)
library(sqldf)
x<-data.frame(orig=as.character(sample(LETTERS[1:10], 100, replace=TRUE)), dest=as.character(sample(LETTERS[1:10], 100, replace=TRUE)))
x$orig<-as.character(x$orig)
x$dest<-as.character(x$dest)

x1<-sort(unique(c(x[,1], x[,2])))

x_ind<-data.frame(loc=x1, ind=1:length(x1))

xx<-sqldf("select a.*, b.ind as orig_ind from x as a left join x_ind as b on a.orig=b.loc")
xxx<-sqldf("select a.*, b.ind as dest_ind from xx as a left join x_ind as b on a.dest=b.loc")

comb<-function(orig, dest, orig_ind, dest_ind)
  {
  if (orig_ind<=dest_ind)
  {
    out<-paste(orig, dest, sep="-")
  } else 
  {
    out<-paste(dest, orig, sep="-")
  }
  return(out)
}

orig_dest<-apply(xxx, 1, function(x) comb(x[1], x[2], x[3], x[4]))
xxxx<-data.frame(xxx, orig_dest)