Elegant indexing up to end of vector/matrix - r

Is it possible in R to say - I want all indices from position i to the end of vector/matrix?
Say I want a submatrix from 3rd column onwards. I currently only know this way:
A = matrix(rep(1:8, each = 5), nrow = 5) # just generate some example matrix...
A[,3:ncol(A)] # get submatrix from 3rd column onwards
But do I really need to write ncol(A)? Isn't there any elegant way how to say "from the 3rd column onwards"? Something like A[,3:]? (or A[,3:...])?

Sometimes it's easier to tell R what you don't want. In other words, exclude columns from the matrix using negative indexing:
Here are two alternative ways that both produce the same results:
A[, -(1:2)]
A[, -seq_len(2)]
Results:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 4 5 6 7 8
[2,] 3 4 5 6 7 8
[3,] 3 4 5 6 7 8
[4,] 3 4 5 6 7 8
[5,] 3 4 5 6 7 8
But to answer your question as asked: Use ncol to find the number of columns. (Similarly there is nrow to find the number of rows.)
A[, 3:ncol(A)]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 4 5 6 7 8
[2,] 3 4 5 6 7 8
[3,] 3 4 5 6 7 8
[4,] 3 4 5 6 7 8
[5,] 3 4 5 6 7 8

For rows (not columns as per your example) then head() and tail() could be utilised.
A <- matrix(rep(1:8, each = 5), nrow = 5)
tail(A, 3)
is almost the same as
A[3:dim(A)[1],]
(the rownames/indices printed are different is all).
Those work for vectors and data frames too:
> tail(1:10, 4)
[1] 7 8 9 10
> tail(data.frame(A = 1:5, B = 1:5), 3)
A B
3 3 3
4 4 4
5 5 5
For the column versions, you could adapt tail(), but it is a bit trickier. I wonder if NROW() and NCOL() might be useful here, rather than dim()?:
> A[, 3:NCOL(A)]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 4 5 6 7 8
[2,] 3 4 5 6 7 8
[3,] 3 4 5 6 7 8
[4,] 3 4 5 6 7 8
[5,] 3 4 5 6 7 8
Or flip this on its head and instead of asking R for things, ask it to drop things instead. Here is a function that encapsulates this:
give <- function(x, i, dimen = 1L) {
ind <- seq_len(i-1)
if(isTRUE(all.equal(dimen, 1L))) { ## rows
out <- x[-ind, ]
} else if(isTRUE(all.equal(dimen, 2L))) { ## cols
out <- x[, -ind]
} else {
stop("Only for 2d objects")
}
out
}
> give(A, 3)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 2 3 4 5 6 7 8
[2,] 1 2 3 4 5 6 7 8
[3,] 1 2 3 4 5 6 7 8
> give(A, 3, dimen = 2)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 4 5 6 7 8
[2,] 3 4 5 6 7 8
[3,] 3 4 5 6 7 8
[4,] 3 4 5 6 7 8
[5,] 3 4 5 6 7 8

You can use the following instruction:
A[, 3:length(A[, 1])]

A dplyr readable renewed approach for the same thing:
A %>% as_tibble() %>%
select(-c(V1,V2))
A %>% as_tibble() %>%
select(V3:ncol(A))

Related

Combinations of vector with sub-vector length n

Given a vector, 1:4, and a sequence length, 2, I would like to separate the vector into 'sub-vectors', each with a length of 2, and generate a matrix of all possible combinations of these sub-vectors.
Output would look like this:
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 3 4 1 2
Another example. With vector 1:8 and sub-vector length of 4, output would look like this:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 2 3 4 5 6 7 8
[2,] 5 6 7 8 1 2 3 4
With a vector 1:9 and sub-vector length of 3, output would look like this:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 2 3 4 5 6 7 8 9
[2,] 1 2 3 7 8 9 4 5 6
[3,] 4 5 6 1 2 3 7 8 9
[4,] 4 5 6 7 8 9 1 2 3
[5,] 7 8 9 4 5 6 1 2 3
[6,] 7 8 9 1 2 3 4 5 6
It's a given that the vector length must be divisible by the sub-vector length.
I can answer the whole question, but it will take a bit longer. This should give you the flavour of the answer.
The package combinat has a function called permn which gives you the all the permutations of a vector. You want this, but not quite. What you need is the permutations of all the blocks. So in your first example you have two blocks of length two, and in your second example you have three blocks of length three. If we look at the first, and think about ordering the blocks:
> library(combinat)
> numBlocks = 2
> permn(1:numBlocks)
[[1]]
[1] 1 2
[[2]]
[1] 2 1
So I hope you can see that the first permutation would take the blocks b1 = c(1,2), and b2 = c(3,4) and order them c(b1,b2), and the second would order them c(b2,b1).
Equally if you had three blocks, b1 = 1:3; b2 = 4:6; b3 = 7:9 then
permn(1:3)
[[1]]
[1] 1 2 3
[[2]]
[1] 1 3 2
[[3]]
[1] 3 1 2
[[4]]
[1] 3 2 1
[[5]]
[1] 2 3 1
[[6]]
[1] 2 1 3
gives you the ordering of these blocks. The more general solution is figuring out how to move the blocks around, but that isn't too hard.
Update: Using my multicool package. Note co-lexical ordering (coolex) isn't the order you'd come up with by yourself.
library(multicool)
combs = function(v, blockLength){
if(length(v) %% blockLength != 0){
stop("vector length must be divisible by blockLength")
}
numBlocks = length(v) / blockLength
blockWise = matrix(v, nc = blockLength, byrow = TRUE)
m = initMC(1:numBlocks)
Perms = allPerm(m)
t(apply(Perms, 1, function(p)as.vector(t(blockWise[p,]))))
}
> combs(1:4, 2)
[,1] [,2] [,3] [,4]
[1,] 3 4 1 2
[2,] 1 2 3 4
> combs(1:9, 3)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 7 8 9 4 5 6 1 2 3
[2,] 1 2 3 7 8 9 4 5 6
[3,] 7 8 9 1 2 3 4 5 6
[4,] 4 5 6 7 8 9 1 2 3
[5,] 1 2 3 4 5 6 7 8 9
[6,] 4 5 6 1 2 3 7 8 9

R - sum each element in a vector with each element of other vector

I have two vectors and I want a new vector which elements are the sum of an element of vector 1 and an element of vector 2.
v1<-c(1,2,3,4,5,6)
v2<-c(0,1,1,2,2,1)
for(i in 1:length(v1)){
for(j in 1:length(v2)){
n<-vector()
n<-v1[i]+v2[j]
}
m<-NULL
m[n]<-m
}
When I run the loop, I get m=NULL and n is numeric class with NA. Any idea?
Perhaps we need
tapply(c(v1, v2), c(v1, v2), FUN = sum)
Or just
v1 + v2
Or could be outer
outer(v1, v2, FUN = "+")
If you want to correct your code, you can try something like this:
v1<-c(1,2,3,4,5,6)
v2<-c(0,1,1,2,2,1)
m<-matrix(rep(0,length(v1)*length(v2)), nrow=length(v1))
for(i in 1:length(v1)){
for(j in 1:length(v2)){
m[i,j] <- v1[i]+v2[j]
}
}
m
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 2 3 3 2
[2,] 2 3 3 4 4 3
[3,] 3 4 4 5 5 4
[4,] 4 5 5 6 6 5
[5,] 5 6 6 7 7 6
[6,] 6 7 7 8 8 7
This can also be done this way
outer(v1, v2, FUN='+')
or in this way
matrix(apply(expand.grid(1:length(v1), 1:length(v2))[2:1], 1,
function(x)v1[x[1]]+v2[x[2]]), nrow=length(v1), byrow=TRUE)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 2 3 3 2
[2,] 2 3 3 4 4 3
[3,] 3 4 4 5 5 4
[4,] 4 5 5 6 6 5
[5,] 5 6 6 7 7 6
[6,] 6 7 7 8 8 7

R Equivalent of "end" in MatLab [duplicate]

Is it possible in R to say - I want all indices from position i to the end of vector/matrix?
Say I want a submatrix from 3rd column onwards. I currently only know this way:
A = matrix(rep(1:8, each = 5), nrow = 5) # just generate some example matrix...
A[,3:ncol(A)] # get submatrix from 3rd column onwards
But do I really need to write ncol(A)? Isn't there any elegant way how to say "from the 3rd column onwards"? Something like A[,3:]? (or A[,3:...])?
Sometimes it's easier to tell R what you don't want. In other words, exclude columns from the matrix using negative indexing:
Here are two alternative ways that both produce the same results:
A[, -(1:2)]
A[, -seq_len(2)]
Results:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 4 5 6 7 8
[2,] 3 4 5 6 7 8
[3,] 3 4 5 6 7 8
[4,] 3 4 5 6 7 8
[5,] 3 4 5 6 7 8
But to answer your question as asked: Use ncol to find the number of columns. (Similarly there is nrow to find the number of rows.)
A[, 3:ncol(A)]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 4 5 6 7 8
[2,] 3 4 5 6 7 8
[3,] 3 4 5 6 7 8
[4,] 3 4 5 6 7 8
[5,] 3 4 5 6 7 8
For rows (not columns as per your example) then head() and tail() could be utilised.
A <- matrix(rep(1:8, each = 5), nrow = 5)
tail(A, 3)
is almost the same as
A[3:dim(A)[1],]
(the rownames/indices printed are different is all).
Those work for vectors and data frames too:
> tail(1:10, 4)
[1] 7 8 9 10
> tail(data.frame(A = 1:5, B = 1:5), 3)
A B
3 3 3
4 4 4
5 5 5
For the column versions, you could adapt tail(), but it is a bit trickier. I wonder if NROW() and NCOL() might be useful here, rather than dim()?:
> A[, 3:NCOL(A)]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 4 5 6 7 8
[2,] 3 4 5 6 7 8
[3,] 3 4 5 6 7 8
[4,] 3 4 5 6 7 8
[5,] 3 4 5 6 7 8
Or flip this on its head and instead of asking R for things, ask it to drop things instead. Here is a function that encapsulates this:
give <- function(x, i, dimen = 1L) {
ind <- seq_len(i-1)
if(isTRUE(all.equal(dimen, 1L))) { ## rows
out <- x[-ind, ]
} else if(isTRUE(all.equal(dimen, 2L))) { ## cols
out <- x[, -ind]
} else {
stop("Only for 2d objects")
}
out
}
> give(A, 3)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 2 3 4 5 6 7 8
[2,] 1 2 3 4 5 6 7 8
[3,] 1 2 3 4 5 6 7 8
> give(A, 3, dimen = 2)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 3 4 5 6 7 8
[2,] 3 4 5 6 7 8
[3,] 3 4 5 6 7 8
[4,] 3 4 5 6 7 8
[5,] 3 4 5 6 7 8
You can use the following instruction:
A[, 3:length(A[, 1])]
A dplyr readable renewed approach for the same thing:
A %>% as_tibble() %>%
select(-c(V1,V2))
A %>% as_tibble() %>%
select(V3:ncol(A))

R - Concisely add vector to each vector element

Let's say I have a simple vector
v <- 1:5
I can add the vector to each element within the vector with the following code to generate the resulting matrix.
matrix(rep(v, 5), nrow=5, byrow=T) + matrix(rep(v, 5), nrow=5)
[,1] [,2] [,3] [,4] [,5]
[1,] 2 3 4 5 6
[2,] 3 4 5 6 7
[3,] 4 5 6 7 8
[4,] 5 6 7 8 9
[5,] 6 7 8 9 10
But this seems verbose and inefficient. Is there a more concise way to accomplish this? Perhaps some linear algebra concept that is evading me?
outer should do what you want
outer(v, v, `+`)
# [,1] [,2] [,3] [,4] [,5]
# [1,] 2 3 4 5 6
# [2,] 3 4 5 6 7
# [3,] 4 5 6 7 8
# [4,] 5 6 7 8 9
# [5,] 6 7 8 9 10
Posting this answer not for up votes but to highlight Franks comment. You can use
sapply(v,"+",v)

Draw a Hankel matrix with R

I want to draw a Hankel matrix with R use only matrix(), seq() and rep() function of R. Until now, I draw this in some way:
#Do this exercise with other packages, need to rework
install.packages("matrixcalc")
library(matrixcalc)
E1 <- hankel.matrix( 5, seq( 1, 9 ) )
print(E1)
#Use matrix() only, not efficient
E2 <- matrix(c(1,2,3,4,5,2,3,4,5,6,3,4,5,6,7,4,5,6,7,8,5,6,7,8,9), ncol=5)
print(E2)
#Use seq() but not worked
E3 <- matrix(c(seq(1:5),seq(2:6),seq(3:7),seq(4:8),seq(5:9)), ncol=5)
print(E3)
E1 used a library to draw a Hankel matrix and in E2, I tried to put the number manually to draw one but it will take a lot of time if I want a new big matrix. I tried to use seq() but it not worked. It will draw like this:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
I am still very new with R so every idea is welcome.
You can do this :
matrix(rep(1:5,5)+rep(0:4,each=5),ncol=5)
# [,1] [,2] [,3] [,4] [,5]
# [1,] 1 2 3 4 5
# [2,] 2 3 4 5 6
# [3,] 3 4 5 6 7
# [4,] 4 5 6 7 8
# [5,] 5 6 7 8 9
Or more elegant but using outer:
outer(0:4,1:5,'+')
EDIT :
the rep solution works like this:
12345 12345 12345 ... (rep times, repeat the vector n times
+ 00000 11111 22222 ... (rep with each , repeat each element n times
= 12345 23456 34567 .....
outer can be tricky at first, maybe this answer here can help you to understand it and to general debug.
I'm reversing the order of the two different solutions so that the qualifying one is at the top:
A general function (meaning on that doesn't depend on the values being sequential) that only uses a couple of extra functions (like c() and "[") to do the work:
N <- c(9L, 7L, 3L, 2L, 1L, 8L, 4L, 5L, 6L, 10L)
hankel2 <- function(N, n){stopifnot(length(N)==2*n);
matrix( rep(N,n)[c(rep(TRUE,n),rep(FALSE,n+1))], n) }
hankel2(N,5)
[,1] [,2] [,3] [,4] [,5]
[1,] 9 7 3 2 1
[2,] 7 3 2 1 8
[3,] 3 2 1 8 4
[4,] 2 1 8 4 5
[5,] 1 8 4 5 6
The trick with that first (of three) efforts was to depend on argument recycling of logical vectors when used inside the "[" function. It creates a gap of n+1 items after choosing n items by indexing with FALSE ( which has the effect of omitting items.)
Embed is a cute little function that has a fairly opaque help file but occasionally delivers very compact code:
> x <- 1:10
> embed (x, 5)[1:5, 5:1]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 2 3 4 5 6
[3,] 3 4 5 6 7
[4,] 4 5 6 7 8
[5,] 5 6 7 8 9
You could make a function:
> hankel <- function( n ) embed(1:(2*n),5)[1:n, n:1]
> hankel(5)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 2 3 4 5 6
[3,] 3 4 5 6 7
[4,] 4 5 6 7 8
[5,] 5 6 7 8 9
(Admittedly not playing by the specifications although I wondered if any of the solutions so far would stand up to a vector that wasn't sequential. This one does:)
> hankel5 <- function( n ) embed(sample(1:10,10),5)[1:n, n:1]
> hankel5(5)
[,1] [,2] [,3] [,4] [,5]
[1,] 3 5 7 9 4
[2,] 5 7 9 4 10
[3,] 7 9 4 10 1
[4,] 9 4 10 1 8
[5,] 4 10 1 8 2
So this is the other general function:
hankel <- function( N, n ) {stopifnot(length(N) == 2*n); embed(N,n)[1:n, n:1]

Resources