If I have a vector c(1,2,3) and another vector of the same length c(1,4,1) . Is there some way to find the minimum of each pair of numbers in the list pair? i.e. have a function that returns c(1,2,1)` I don't want to use any apply function or loops because my vectors will be very big and looping through them would take a long time.
You want pmin():
> x <- c(1,2,3)
> y <- c(1,4,1)
> pmin(x,y)
[1] 1 2 1
Related
I have problem to find first n elements of vector in R.
I have tried prod() function, but question is how first n elements?
How to find product of first n elements of vector in R?
An idea would be to create a function where you determine vector entry and length of desired elements for product formula
# Function
prodFun <- function(x, len){
return(prod(x[1:len]))
}
# Example
x <- 1:10
prodFun(x, 5)
Suppose I have a list of vectors. Suppose further that I would like to have a condition based on their length. That is, I would like my function return an error if the lengths of these vectors are not equal.
For example,
x <- c(1:4)
y <- c(1:5)
z <- c(1:4)
k <- list(x, y, z)
I would like to check that their lengths are equal.
stopifnot(length(k[[1]]) == length (k[[2]]) == length(k[[3]]))
How could I generalize this code and make it works for an arbitrary number of elements of the list?
We can use lengths with unique
stopifnot(length(unique(lengths(k)))==1)
Error: length(unique(lengths(k))) == 1 is not TRUE
The lengths will get the length of each of the vector in the list as a vector, get the unique and check if the length is equal to 1. If it is not i.e. stopifnot, give an error
I just started doing some R script and I can't figure out this problem.
I got a list of vector let say
myListOfVector <- list(
c(1,2),
c(1,2),
c(1,2),
c(1,2)
)
what I want is the sum of each X element of each vector that are in my list base on the position of the element
so that if I have 3 vector that contains (a, b, c), I will get the sum of each a, each b and each c in a list or vector
I know that each vector are the same length
What I seek is something like that
result <- sum(myListOfVector)
# result must be c(4, 8)
Does anybody have an idea ?
The only way I've been able to do it is by using a loop but it take so much time that I can't resign to do it.
I tried some apply and lapply but they don't seem to work like I want it to because all I have is one vector at a time.
Precision :
The list of vector is returned by a function that I can't modify
I need an efficient solution if possible
A list of vectors of the same length can be summed with Reduce:
Reduce(`+`, myListOfVector)
Putting it in a matrix and using colSums or rowSums, as mentioned by #AnandaMahto and #JanLuba, might be faster in some cases; I'm not sure.
Side note. The example in the OP is not a list; instead, it should be constructed like
myListOfVector <- list( # ... changing c to list on this line
c(1,2),
c(1,2),
c(1,2),
c(1,2)
)
you should first convert your list to a matrix:
mymatrix=matrix(myListOfVector,ncol=2,byrow=T)
and then use colSums:
colSums(mymatrix)
I have following problem with R function findInterval()
Given a vector X and a matrix Y, I want to find in which interval lie elements of X. Intervals are constructed, having breakpoints in Y rows. In other words for X = c(2,3) and Y = matrix(c(3,1,4,2,5,4),2,3), the output would be c(0,2). I wrote following code:
X <- c(2,3)
Y <- matrix(c(3,1,4,2,5,4),2,3)
output <- diag(apply(Y,1,function(z)findInterval(X,z)))
and it works. However, I think, it can be optimised, since the apply function returns 2 x 2 matrix (that's why i had to get diagonal of that). Is there a way to do the same, but using function, which will return a vector, taking as an argument my vector X and matrix Y? I perform this operation on high-demensional vectors, so obtaining unnecessary matrixes size 10000 x 10000 is not a good idea imho. To maximize efficiency, I don't want to use loops.
Thanks in advance for any feedback.
You can do
rowSums(X > Y)
# [1] 0 2
I need to calculate the mean (or other summary functions) on the top x and bottom x portions on list of vectors of varying lengths.
Here is a list of 3 vectors of different lengths similar in format with what I am working with:
t <- list(a = exp(-4:3), b = exp(-2:12), c = exp(-5:3))
Ideally, I would like a single vector of numbers for each type of means (I manually ran mean(head(t$a),2)) and mean(tail(t$a),2)) for each vectors):
Ideal output yielding a nameless vector of means of the first two elements from each vector:
[1] 0.2516074 1.859141 0.09256118
Second vector of means for last two entries in each vector:
[1] 1.859141 15064.77 1.859141
Looking for a clever lapply-type construct to get a vector of numbers for each means without the attached names (in this case a,b,c). Thanks!
What about
n = 2
v = lapply(t, function(i) mean(head(i, n)))
The variable v is list. So to get a vector, just use unlist
v = unlist(v)
To extract the numbers use as.vector
as.vector(v)
For the tail, just use
lapply(t, function(i) mean(tail(i, n)))
Using sapply you can wrap this in a function:
sapply(dat,function(x,length=2)
c(mean(head(x,length)),mean(head(x,length))))
# a b c
# [1,] 0.03405135 0.2516074 0.01252679
# [2,] 0.03405135 0.2516074 0.01252679