The command
matrix(sample.int(12, 9*12, TRUE), 9, 12)
generates an integer random matrix (9 rows and 12 columns) with integer values from 1 to 12. I wonder if there is a version of this code that generates a matrix whose rows are integer random rows with value from 1 to 12 (without repetition). I was able to find a "trivial" answer to this question; with
matrix(sample.int(m, 1*12), 9, 12, byrow=TRUE)
I obtain a matrix of this kind, but the rows are all equal to each other (this is the same row repeated 9 times).
The replicate function (which repeats an operation like sample(12) a specified number of times) returns a matrix whose column major orientation can be flipped to your desired row orientation with t:
t( replicate(9, {sample(12)} ) )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 9 11 5 3 4 6 2 8 10 12 7 1
[2,] 4 5 12 6 8 2 9 1 11 10 7 3
[3,] 9 8 10 12 2 6 3 7 4 1 11 5
[4,] 4 9 1 2 6 11 8 5 7 3 12 10
[5,] 1 2 4 5 11 6 3 8 10 9 12 7
[6,] 4 8 10 12 5 9 2 7 11 1 3 6
[7,] 5 7 8 4 1 6 10 11 2 3 12 9
[8,] 2 4 10 1 12 5 7 6 11 3 8 9
[9,] 2 7 9 11 8 1 12 10 6 5 3 4
The replicate function is used in a lot of simulation code.
Related
This is what I know in Matlab and want do same in r programming
A=[1 2 3;4 5 6; 7 8 9];
A =
1 2 3
4 5 6
7 8 9
[m,n]=size(A)
m=3
n=3
so here I have two distinct variables to which the dimension or size of 2D matrix is assigned automatically
> x<-c(1:10)
> x
[1] 1 2 3 4 5 6 7 8 9 10
> A=matrix(0,10,10)
> A=Toeplitz(x,c(x[1],rev(x[-1])))
> A
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 10 9 8 7 6 5 4 3 2
[2,] 2 1 10 9 8 7 6 5 4 3
[3,] 3 2 1 10 9 8 7 6 5 4
[4,] 4 3 2 1 10 9 8 7 6 5
[5,] 5 4 3 2 1 10 9 8 7 6
[6,] 6 5 4 3 2 1 10 9 8 7
[7,] 7 6 5 4 3 2 1 10 9 8
[8,] 8 7 6 5 4 3 2 1 10 9
[9,] 9 8 7 6 5 4 3 2 1 10
[10,] 10 9 8 7 6 5 4 3 2 1
> n=size(A)
> n
[1] 10 10
>[ m,n]=size(A)
this is not working so is there any way to assign the size of the 2Dmatrix to two distinct variable m and n in r.I am learning r programming and need help
Is there a function in R which switches the first element with the last one in a vector? I have a for loop which need that reordering. From:
months = seq(1:12)
[1] 1 2 3 4 5 6 7 8 9 10 11 12
I would like to have:
[1] 12 1 2 3 4 5 6 7 8 9 10 11
and then again:
[1] 11 12 1 2 3 4 5 6 7 8 9 10
...
until the 12th position.
If you need a matrix output
cbind(c(months),embed(c(months, months), 12)[-13,-12])
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
# [1,] 1 12 11 10 9 8 7 6 5 4 3 2
# [2,] 2 1 12 11 10 9 8 7 6 5 4 3
# [3,] 3 2 1 12 11 10 9 8 7 6 5 4
# [4,] 4 3 2 1 12 11 10 9 8 7 6 5
# [5,] 5 4 3 2 1 12 11 10 9 8 7 6
# [6,] 6 5 4 3 2 1 12 11 10 9 8 7
# [7,] 7 6 5 4 3 2 1 12 11 10 9 8
# [8,] 8 7 6 5 4 3 2 1 12 11 10 9
# [9,] 9 8 7 6 5 4 3 2 1 12 11 10
#[10,] 10 9 8 7 6 5 4 3 2 1 12 11
#[11,] 11 10 9 8 7 6 5 4 3 2 1 12
#[12,] 12 11 10 9 8 7 6 5 4 3 2 1
Or another approached suggested by #Marat Talipov
z <- length(months)
i <- rep(seq(z),z) + rep(seq(z),each=z) - 1
matrix(months[ifelse(i>z,i-z,i)],ncol=z)
I'm afraid that you have to come up with a home-made function, something like this one:
rotate <- function(v,i=1) {
i <- i %% length(v)
if (i==0) return(v)
v[c(seq(i+1,length(v)),seq(i))]
}
Couple of examples:
v <- seq(12)
rotate(v,1)
# [1] 2 3 4 5 6 7 8 9 10 11 12 1
rotate(v,-1)
# [1] 12 1 2 3 4 5 6 7 8 9 10 11
You can also use tail and head functions:
x = c(tail(x,n), head(x,-n))
and modify n to rotate n times
The permute package can do this for you:
ap <- allPerms(length(months),
control = how(within = Within(type = "series"),
observed = TRUE))
ap[rev(seq_len(nrow(ap))), ]
(because of the way allPerms() does its work, we need to reverse the order of the rows, which is what the last line does.)
This gives:
> ap[rev(seq_len(nrow(ap))), ]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 2 3 4 5 6 7 8 9 10 11 12
[2,] 12 1 2 3 4 5 6 7 8 9 10 11
[3,] 11 12 1 2 3 4 5 6 7 8 9 10
[4,] 10 11 12 1 2 3 4 5 6 7 8 9
[5,] 9 10 11 12 1 2 3 4 5 6 7 8
[6,] 8 9 10 11 12 1 2 3 4 5 6 7
[7,] 7 8 9 10 11 12 1 2 3 4 5 6
[8,] 6 7 8 9 10 11 12 1 2 3 4 5
[9,] 5 6 7 8 9 10 11 12 1 2 3 4
[10,] 4 5 6 7 8 9 10 11 12 1 2 3
[11,] 3 4 5 6 7 8 9 10 11 12 1 2
[12,] 2 3 4 5 6 7 8 9 10 11 12 1
Technically this only works because months is the vector 1:12 and allPerms() returns a permutation matrix of the indices of the thing you want permuted. For different inputs, use ap to index into the thing you want to permute
perms <- ap
perms[] <- months[ap[rev(seq_len(nrow(ap))), ]]
perms
I found this while searching for a similar approach.
Selecting rows with same result in different columns in R
Is there a way to search within a range of columns? Playing off the example in the link, what if instead of catch[catch$tspp.name == catch$elasmo.name,], is it possible to do this?
catch[catch$tspp.name == c[23:56],] where R would search for values within columns 23 to 56 that match the tspp value?
Thanks in advance and please let me know whether it's better to post an independent question on a topic related to a previous post or to insert a follow up question within the aforementioned post.
Here's one way to do it. This finds rows of X where the first column appears in columns 2 through 9.
> set.seed(1)
> X<-matrix(sample(10,100,T),10)
> X
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3 3 10 5 9 5 10 4 5 3
[2,] 4 2 3 6 7 9 3 9 8 1
[3,] 6 7 7 5 8 5 5 4 4 7
[4,] 10 4 2 2 6 3 4 4 4 9
[5,] 3 8 3 9 6 1 7 5 8 8
[6,] 9 5 4 7 8 1 3 9 3 8
[7,] 10 8 1 8 1 4 5 9 8 5
[8,] 7 10 4 2 5 6 8 4 2 5
[9,] 7 4 9 8 8 7 1 8 3 9
[10,] 1 8 4 5 7 5 9 10 2 7
> X[rowSums(X[,1]==X[,2:9])>0,]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3 3 10 5 9 5 10 4 5 3
[2,] 3 8 3 9 6 1 7 5 8 8
[3,] 9 5 4 7 8 1 3 9 3 8
[4,] 7 4 9 8 8 7 1 8 3 9
I want to calculate all permutations of a blocked design suitable for a Friedman test. Consider the following example:
thedata <- data.frame(
score = c(replicate(4,sample(1:3))),
judge = rep(1:4,each=3),
wine = rep.int(1:3,4)
)
Four judges ranked 3 wines and now I want to calculate every possible permutation within the data for every judge. I expect to see 1,296 permutations, as also given by:
require(permute)
CTRL <- how(within=Within("free"),
plots=Plots(strata=factor(thedata$judge)),
complete=TRUE,maxperm=1e9)
numPerms(12,CTRL)
However, allPerms(12,control=CTRL) produces the following error:
Error in (function (..., deparse.level = 1) :
number of rows of matrices must match (see arg 2)
I tried using the block argument, but it simply returns a matrix that repeats 4 times a matrix with the 6 possible permutations of 3 values:
CTRL <- how(within=Within("free"),
blocks=factor(thedata$judge),
complete=TRUE,maxperm=1e9)
allPerms(12,control=CTRL)
IMPORTANT NOTE:
I do have a custom function to obtain the result, using an adaptation of expand.grid() with permn() from the combinat package. I'm interested in where I misunderstand the permute package, not how I can calculate all these permutations myself.
The examples provided by #Joris identify two bugs in allPerms() that were not picked up by the current set of examples or unit tests (that'll be fixed soon too!).
The first issue is an obscure bug that I'll need a bit of time to think through a fix for. I have now implemented fixes for this bug too. Version 0.8-3 of permute now happily handles the Plots version of #joris' question:
R> p <- allPerms(12,control=CTRL)
R> dim(p)
[1] 1295 12
R> head(p)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 2 3 4 5 6 7 8 9 10 12 11
[2,] 1 2 3 4 5 6 7 8 9 11 10 12
[3,] 1 2 3 4 5 6 7 8 9 11 12 10
[4,] 1 2 3 4 5 6 7 8 9 12 10 11
[5,] 1 2 3 4 5 6 7 8 9 12 11 10
[6,] 1 2 3 4 5 6 7 9 8 10 11 12
R> packageVersion("permute")
[1] ‘0.8.3’
The second is an oversight. allPerms() generates permutation indices, but internally it works block by block. In the case #Joris reported, each block has 3 observations and hence 6 permutations of the indices 1:3. Once these permutation indices have been created, the code should have used them to index the row indices of the original data for each block. allPerms() was doing this for every conceivable combination of permutation types except the simple random permutation within blocks case. r2838 fixes this issue.
allPerms() was also not replicating each within-block permutation matrix to match each combination of rows in the other within-block permutation matrices. This requires an operation like expand.grid() but on the within-block permutation matrices. r2839 fixes this particular issue.
allPerms() works this way because it does not expect the within-block samples to be located contiguously within the original data series.
This second bug was fixed via r2838 and r2839 in the SVN sources on R-Forge.
R> require(permute)
Loading required package: permute
R> CTRL <- how(within=Within("free"),
+ blocks=factor(thedata$judge),
+ complete=TRUE,maxperm=1e9,
+ observed = TRUE)
R> numPerms(12,CTRL)
[1] 1296
R> tmp <- allPerms(12,control=CTRL)
R> dim(tmp)
[1] 1296 12
R> head(tmp)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 2 3 4 5 6 7 8 9 10 11 12
[2,] 1 2 3 4 5 6 7 8 9 10 12 11
[3,] 1 2 3 4 5 6 7 8 9 11 10 12
[4,] 1 2 3 4 5 6 7 8 9 11 12 10
[5,] 1 2 3 4 5 6 7 8 9 12 10 11
[6,] 1 2 3 4 5 6 7 8 9 12 11 10
R> tail(tmp)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1291,] 3 2 1 6 5 4 9 8 7 10 11 12
[1292,] 3 2 1 6 5 4 9 8 7 10 12 11
[1293,] 3 2 1 6 5 4 9 8 7 11 10 12
[1294,] 3 2 1 6 5 4 9 8 7 11 12 10
[1295,] 3 2 1 6 5 4 9 8 7 12 10 11
[1296,] 3 2 1 6 5 4 9 8 7 12 11 10
Is there any function in R to find most frequently occuring element in matrix??I Have a matrix containing image pixels.I want to find which image pixel occur most frequently in the image matrix.I dont want to use the for loops since it would be very time taking to iterate over all the pixels of an image.
Set up some test data.
> (image = matrix(sample(1:10, 100, replace = TRUE), nrow = 10))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 4 4 2 7 2 2 3 8 2 5
[2,] 7 3 2 6 6 5 7 8 1 3
[3,] 7 5 7 9 4 9 4 8 2 7
[4,] 5 3 4 2 1 5 9 10 9 5
[5,] 9 10 7 2 7 4 9 1 1 9
[6,] 2 3 5 1 2 8 1 5 9 4
[7,] 5 4 10 5 9 10 1 6 1 10
[8,] 6 3 9 7 1 1 9 2 1 7
[9,] 5 9 4 8 9 9 5 10 5 4
[10,] 10 1 4 7 3 2 3 5 4 5
Do it manually.
> table(image)
image
1 2 3 4 5 6 7 8 9 10
12 12 8 12 15 4 11 5 14 7
Here we can see that the value 5 appeared most often (15 times). To get the same results programmatically:
> which.max(table(image))
5
5
Get mode (or majority value) in 1 line of code
using set.seed to generate same random matrix
> set.seed(123)
> image = matrix(sample(1:10, 100, replace = TRUE), nrow = 10)
> image
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 3 10 9 10 2 1 7 8 3 2
[2,] 8 5 7 10 5 5 1 7 7 7
[3,] 5 7 7 7 5 8 4 8 5 4
[4,] 9 6 10 8 4 2 3 1 8 7
[5,] 10 2 7 1 2 6 9 5 2 4
[6,] 1 9 8 5 2 3 5 3 5 2
[7,] 6 3 6 8 3 2 9 4 10 8
[8,] 9 1 6 3 5 8 9 7 9 1
[9,] 6 4 3 4 3 9 8 4 9 5
[10,] 5 10 2 3 9 4 5 2 2 6)
Mode value of matrix (if tie, its gives minimum tie value)
> names(which.max(table(image)))
[1] "5"
I do not know any function to do that directly but you can use these functions:
sort(table(as.vector(Matrix))