Making a subvector, that only keeps elements divisible by three - r

so I am currently struggling with starting out in R. I had a task of creating a vector v=c(1,3,5,7,8,9,11,13,15,17,19,21), and then to create a subvector, that only keeps the elements of v that are divisible by three.
I suppose i would have to use the %% operator but I am not really sure how to get it to kind of pick and choose, instead of just dividing every element by three. I also tried to create a vector of just threes in order to the divide the original vector by that... no suprise that didnt work lol.
Any help appreciated, i just want to get to know how to use the different operators and commands.

Create vector
x <- c(1,3,5,7,8,9,11,13,15,17,19,21)
Filter elements that can divided by 3
x[(x %% 3) == 0]
Result
[1] 3 9 15 21

If you want to keep only the elements divisable by 3, you can do it with purrr::keep and modulo x%%y.
library(purrr)
v %>% keep(v%%3==0)
#OR simply
keep(v, v%%3==0)
[1] 3 9 15 21

Related

Calculate product of all following vector elements in R

I have some arbitrary vector such as
v <- seq(1,5)
For every index of v I now need to compute the product of all following elements of v.
Here, the result would be a vector w=(5*4*3*2,5*4*3,5*4,5,1) but I need a general algorithm for this. I am trying to avoid a loop (which is the obvious solution).
You can use cumprod with rev:
c(rev(cumprod(rev(v[-1]))), 1)
#[1] 120 60 20 5 1

Count number of identical values between two vectors

a<-c(19,24,34,47,47,47)
b<-c(3,14,24,25,47,47)
I want to know how many values in a match those in b, however i'm running into issues - when there are duplicate numbers present in both vectors. My desired answer for the above example would be 3 - because 24,47,47 - are shared between the two vectors.
If I use intersect:
intersect(a,b)
[1] 24 47
The 2nd matching 47 is ignored.
If I use %in%:
length(which(a %in% b))
[1] 4
The extra 47 in a is also counted.
I realise that I can do:
length(which(b %in% a))
[1] 3
However, I may also have cases where there is an extra matching value in b instead of a and so %in% is also not useful. For example:
a<-c(19,24,34,7,47,47)
b<-c(3,14,24,47,47,47)
length(which(b %in% a))
[1] 4 (I want the answer to still be 3)
So, without rearranging which vector comes first in the %in% function, for each test - I cannot figure out how to do this. Can somebody show me how?
How about:
sum(pmin(
table(a[a %in% intersect(a, b)]),
table(b[b %in% intersect(a, b)])
))
We make table()s of the chunks of a, b that are common to both, then we take the smallest numbers from those tables and add them up.

Muliplying Elements of a Vector one more each time

I am trying to create a vector from another vector where I multiply the numbers in the vector one more each time.
For example if I had (1,2,3) the new vector would be (1, 1 x 2, 1 x 2 x 3)=(1,2,6)
I tried to create a loop for this as seen below. It seems to work for whole numbers but not decimals. I am not sure why.
x <- c(0.99,0.98,0.97,0.96,0.95)
for(i in 1:5){x[i]=prod(x[1:i])}
The result given is 0.9900000 0.9702000 0.9316831 0.8590845 0.7303385
which is incorrect as prod(x) = 0.8582777. Which is not the same as the last element of the vector.
Does anyone know why this is the case? Or have a suggestion for improvement in my code to get the correct answer.
test<-c(1,2,3)
cumprod(test)
[1] 1 2 6
As #akrun suggests, one can achieve the same with:
Reduce("*", test, accumulate = TRUE)

How to find common elements on two different length vectors in R?

I need to find common elements in 2 different length vectors.
For example, I have a vector A with 10 elements, and a vector B with 3 elements.
I need get the position of which elements in A is equal to B.
A=c(1,2,45,3,10,5,11,13,6,7)
B=c(45,3,10)
C would be [3,4,5]
I have already tried "match" and "intercept" functions, but no success :(
Thanks a lot! :)
You can use which function.
> which(A %in% B)
[1] 3 4 5

i not showing up as number in loop

so I have a loop that finds the position in the matrix where there is the largest difference in consecutive elements. For example, if thematrix[8] and thematrix[9] have the largest difference between any two consecutive elements, the number given should be 8.
I made the loop in a way that it will ignore comparisons where one of the elements is NaN (because I have some of those in my data). The loop I made looks like this.
thenumber = 0 #will store the difference
for (i in 1:nrow(thematrix) - 1) {
if (!is.na(thematrix[i]) & !is.na(thematrix[i + 1])) {
if (abs(thematrix[i] - thematrix[i + 1]) > thenumber) {
thenumber = i
}
}
}
This looks like it should work but whenever I run it
Error in if (!is.na(thematrix[i]) & !is.na(thematrix[i + 1])) { :
argument is of length zero
I tried this thing but with a random number in the brackets instead of i and it works. For some reason it only doesn't work when I use the i specified in the beginning of the for-loop. It doesn't recognize that i represents a number. Why doesn't R recognize i?
Also, if there's a better way to do this task I'd appreciate it greatly if you could explain it to me
You are pretty close but when you call i in 1:nrow(thematrix) - 1 R evaluates this to make i = 0 which is what causes this issue. I would suggest either calling i in 1:nrow(thematrix) or i in 2:nrow(thematrix) - 1 to start your loop at i = 1. I think your approach is generally pretty intuitive but one suggestion would be to frequently use the print() function to evaluate how i changes over the course of your function.
The issue is that the : operator has higher precedence than -; you just need to use parentheses around (nrow(thematrix)-1). For example,
thematrix <- matrix(1:10, nrow = 5)
##
wrong <- 1:nrow(thematrix) - 1
right <- 1:(nrow(thematrix) - 1)
##
R> wrong
#[1] 0 1 2 3 4
R> right
#[1] 1 2 3 4
Where the error message is coming from trying to access the zero-th element of thematrix:
R> thematrix[0]
integer(0)
The other two answers address your question directly, but I must say this is about the worst possible way to solve this problem in R.
set.seed(1) # for reproducible example
x <- sample(1:10,10) # numbers 1:10 in random order
x
# [1] 3 4 5 7 2 8 9 6 10 1
which.max(abs(diff(x)))
# [1] 9
The diff(...) function calculates sequential differences, and which.max(...) identifies the element number of the maximum value in a vector.

Resources