R 将数据拆分为不相交集
我有一个矩阵200x3,我想把它分成3个随机选择的不相交集。我怎样才能实现它 我试着通过sample方法来实现,但是sample方法只接受向量,输出并不是我矩阵的一部分 因此,这是我的矩阵:R 将数据拆分为不相交集,r,disjoint-sets,R,Disjoint Sets,我有一个矩阵200x3,我想把它分成3个随机选择的不相交集。我怎样才能实现它 我试着通过sample方法来实现,但是sample方法只接受向量,输出并不是我矩阵的一部分 因此,这是我的矩阵: X1 X2 Y 1 -3.381342627 1.037658397 0 2 3.329754336 1.964180648 0 3 1.760001645 -3.414310545 0 4 -2.450315854 -2.299838
X1 X2 Y
1 -3.381342627 1.037658397 0
2 3.329754336 1.964180648 0
3 1.760001645 -3.414310545 0
4 -2.450315854 -2.299838395 0
5 -3.334593596 0.069458604 0
6 1.708921101 -2.333932571 0
7 -2.650506645 0.348985289 0
8 -2.935307106 -0.402072990 0
9 2.867566309 -3.217712074 0
10 3.617603017 1.956535384 0
我想这样分成3组:行号必须是随机选择的。我希望能够给出集合的大小。例如,在本例中,4 2
9 2.867566309 -3.217712074 0
3 1.760001645 -3.414310545 0
1 -3.381342627 1.037658397 0
2 3.329754336 1.964180648 0
5 -3.334593596 0.069458604 0
8 -2.935307106 -0.402072990 0
4 -2.450315854 -2.299838395 0
6 1.708921101 -2.333932571 0
10 3.617603017 1.956535384 0
7 -2.650506645 0.348985289 0
这是一种方法
# a matrix with 3 columns
m <- matrix(runif(300), ncol=3)
# split into a list of dataframes (of course, you can convert back to matrices)
m_split <- split(as.data.frame(m), sample(1:3, size=nrow(m), replace=TRUE))
# count nr of rows
sapply(m_split, nrow)
# Or, as in the comment below, split by given number of rows per split
nsplit <- c(30,30,40)
m_split2 <- split(as.data.frame(m), rep(1:3, nsplit))
这是一种方法
# a matrix with 3 columns
m <- matrix(runif(300), ncol=3)
# split into a list of dataframes (of course, you can convert back to matrices)
m_split <- split(as.data.frame(m), sample(1:3, size=nrow(m), replace=TRUE))
# count nr of rows
sapply(m_split, nrow)
# Or, as in the comment below, split by given number of rows per split
nsplit <- c(30,30,40)
m_split2 <- split(as.data.frame(m), rep(1:3, nsplit))
我已经解决了这可能不是最好的方法,但解决方法如下:
nsamples= nrow(data)
//first take a random numbers; %40 of total number of samples
sampleInd = sample(nsamples,0.4*nsamples)
//construct first set via the half of taken indexes
valInd = sampleInd[1:floor(length(sampleInd)/2)]
valSet = dat[valInd,]
//other half
testInd = sampleInd[(floor(length(sampleInd)/2)+1):length(sampleInd)]
testSet = dat[testInd,]
//unused %60
trainSet = dat[-sampleInd,]
ntrain = nrow(trainSet)
程序可以根据您的意愿进行更改。其思想是通过函数样本按指数划分矩阵。然后使用指数来获取实际矩阵。我已经解决了这可能不是最好的方法,但解决方法如下:
nsamples= nrow(data)
//first take a random numbers; %40 of total number of samples
sampleInd = sample(nsamples,0.4*nsamples)
//construct first set via the half of taken indexes
valInd = sampleInd[1:floor(length(sampleInd)/2)]
valSet = dat[valInd,]
//other half
testInd = sampleInd[(floor(length(sampleInd)/2)+1):length(sampleInd)]
testSet = dat[testInd,]
//unused %60
trainSet = dat[-sampleInd,]
ntrain = nrow(trainSet)
程序可以根据您的意愿进行更改。其思想是通过函数样本按指数划分矩阵。然后使用索引获取实际矩阵。我在评论中提到的想法:
# shuffle rows
rows = sample(nrow(m))
# split any way you like, e.g. 4/4/rest
rows.split = split(rows, c(rep(1,4), rep(2,4), rep(3,nrow(m) - 4 - 4)))
# subset the matrix
lapply(rows.split, function(x) m[x,])
我在评论中提到的想法:
# shuffle rows
rows = sample(nrow(m))
# split any way you like, e.g. 4/4/rest
rows.split = split(rows, c(rep(1,4), rep(2,4), rep(3,nrow(m) - 4 - 4)))
# subset the matrix
lapply(rows.split, function(x) m[x,])
3个随机集必须具有相同的维度吗?是的,它们都应该有3列。因此矩阵和行是相等的?如果没有余数,200是不能被3整除的。我不知道你想要多少行。您想要一个可以指定大小的函数,还是希望它们的大小尽可能相等,还是大小是静态的?使用sample将所有行索引洗牌,然后按任意方式拆分并从原始矩阵中选择。三个随机集的大小必须相同?是的,它们都应该有三列。因此矩阵和行是相等的?如果没有余数,200是不能被3整除的。我不知道你想要多少行。您想要一个可以指定大小的函数,还是希望它们的大小尽可能相等,还是大小是静态的?使用sample将所有行索引洗牌,然后按您喜欢的方式拆分,并从原始矩阵中选择Hanks!如何给出每个拆分数据帧的行数?谢谢!如何给出每个拆分数据帧的行数?