Find a vector given certain criteria in R - r

I was searching online if it is possible to create a vector given certain conditions, such as it must contain 2 and 6 but not 5 and 1, also that it is in a specific range (2 000 000-4 999 999), and also that it must be even.
I have genuinely no idea about how to give these commands to R even if I know the basic functions to create a vector.
Thanks in advance for your time and for the big help

You can try the code below
# create a sequence from 2000000 to 4999999
v <- 2e6:(5e6 - 1)
# filter the sequence with given criteria
v[grepl("(2.*6)|(6.*2)", v) & !grepl("(1.*5)|(5.*1)", v)]

You can create it using "seq" function.
seq(from = 2, to = 7, by = 2)
#> [1] 2 4 6
Then use "setdiff" function to remove specific values you dont need.
remove <- c(2)
#> a
[1] 2 4 6
#> setdiff(a, remove)
[1] 4 6

Related

Looping through items on a list in R

this may be a simple question but I'm fairly new to R.
What I want to do is to perform some kind of addition on the indexes of a list, but once I get to a maximum value it goes back to the first value in that list and start over from there.
for example:
x <-2
data <- c(0,1,2,3,4,5,6,7,8,9,10,11)
data[x]
1
data[x+12]
1
data[x+13]
3
or something functionaly equivalent. In the end i want to be able to do something like
v=6
x=8
y=9
z=12
values <- c(v,x,y,z)
data <- c(0,1,2,3,4,5,6,7,8,9,10,11)
set <- c(data[values[1]],data[values[2]], data[values[3]],data[values[4]])
set
5 7 8 11
values <- values + 8
set
1 3 4 7
I've tried some stuff with additon and substraction to the lenght of my list but it does not work well on the lower numbers.
I hope this was a clear enough explanation,
thanks in advance!
We don't need a loop here as vectors can take vectors of length >= 1 as index
data[values]
#[1] 5 7 8 11
NOTE: Both the objects are vectors and not list
If we need to reset the index
values <- values + 8
ifelse(values > length(data), values - length(data) - 1, values)
#[1] 1 3 4 7

More than one value for "each" argument in "rep" function?

How to assign more than one value for "each" argument in "rep" function in R?
A trivial example, where each value in a vector is 3-times repeated in a row:
a <- seq(2,6,2)
rep (a,each = 3)
However, if I add more than one value in "each" argument in order to change the number of repetition of each value, it doesn't work properly:
rep (a, each = c(2,4,7))
How to solve it? Thank you in advance.
Depending on what you think the output should be, I'm guessing you want the times= parameter:
rep (a, times = c(2, 4, 7))
# [1] 2 2 4 4 4 4 6 6 6 6 6 6 6
See ?rep for the difference

R: generate all permutations of vector without duplicated elements

Is there a straightforward way to generate all possible permutations of a vector of integers (1 to max 999) that specifically excludes duplicated elements?
For example, for a vector with three elements in a range of 1 to 9 the sequence 1 2 3 would be acceptable, as would 1 2 9 but 1 2 2 would be invalid. The sequence must contain exactly n elements (in this case, three). EDIT: to avoid confusion, the order is significant, so 1 2 9 and 9 2 1 are both valid and required.
There are many questions on permutations and combinations using R on SO (such as this and this) but none that seem to fit this particular case. I'm hoping there's an obscure base R or package function out there that will take care of it without me having to write a graceless function myself.
Using gtools package:
require(gtools)
permutations(n = 9, r = 3, v = 1:9)
# n -> size of source vector
# r -> size of target vector
# v -> source vector, defaults to 1:n
# repeats.allowed = FALSE (default)
utils::combn ; combinat::combn or combinat::permn are alternatives.
EDIT: This is not what the OP asked for, but I leave this answer, to avoid confusion.
My math is a little bit rusty, but i think you are describing combinations, not permutations. The base functioncombn() returns combinations.
I illustrate with a manageable set - all combinations of length 3, from the vector 1:4:
combn(4, 3)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 2
[2,] 2 2 3 3
[3,] 3 4 4 4
The difference between combinations and permutations is that in combinations the order doesn't matter. So, (2, 3, 4) and (4, 3, 2) is the same combination, but different permutations.

Determining minimum values in a vector in R

I need some help in determining more than one minimum value in a vector. Let's suppose, I have a vector x:
x<-c(1,10,2, 4, 100, 3)
and would like to determine the indexes of the smallest 3 elements, i.e. 1, 2 and 3. I need the indexes of because I will be using the indexes to access the corresponding elements in another vector. Of course, sorting will provide the minimum values but I want to know the indexes of their actual occurrence prior to sorting.
In order to find the index try this
which(x %in% sort(x)[1:3]) # this gives you and index vector
[1] 1 3 6
This says that the first, third and sixth elements are the first three lowest values in your vector, to see which values these are try:
x[ which(x %in% sort(x)[1:3])] # this gives the vector of values
[1] 1 2 3
or just
x[c(1,3,6)]
[1] 1 2 3
If you have any duplicated value you may want to select unique values first and then sort them in order to find the index, just like this (Suggested by #Jeffrey Evans in his answer)
which(x %in% sort(unique(x))[1:3])
I think you mean you want to know what are the indices of the bottom 3 elements? In that case you want order(x)[1:3]
You can use unique to account for duplicate minimum values.
x<-c(1,10,2,4,100,3,1)
which(x %in% sort(unique(x))[1:3])
Here's another way with rank that includes duplicates.
x <- c(x, 3)
# [1] 1 10 2 4 100 3 3
which(rank(x, ties.method='min') <= 3)
# [1] 1 3 6 7

Subsetting R array: dimension lost when its length is 1

When subsetting arrays, R behaves differently depending on whether one of the dimensions is of length 1 or not. If a dimension has length 1, that dimension is lost during subsetting:
ax <- array(1:24, c(2,3,4))
ay <- array(1:12, c(1,3,4))
dim(ax)
#[1] 2 3 4
dim(ay)
#[1] 1 3 4
dim(ax[,1:2,])
#[1] 2 2 4
dim(ay[,1:2,])
#[1] 2 4
From my point of view, ax and ay are the same, and performing the same subset operation on them should return an array with the same dimensions. I can see that the way that R is handling the two cases might be useful, but it's undesirable in the code that I'm writing. It means that when I pass a subsetted array to another function, the function will get an array that's missing a dimension, if I happened to reduce a dimension to length 1 at an earlier stage. (So in this case R's flexibility is making my code less flexible!)
How can I prevent R from losing a dimension of length 1 during subsetting? Is there another way of indexing? Some flag to set?
As you've found out by default R drops unnecessary dimensions. Adding drop=FALSE while indexing can prevent this:
> dim(ay[,1:2,])
[1] 2 4
> dim(ax[,1:2,])
[1] 2 2 4
> dim(ay[,1:2,,drop = F])
[1] 1 2 4

Resources