I want to pick elements from a vector and exclude 3 values by what position they got.
I know about the x[-n] function to exclude a single value but I donĀ“t know how to exclude more than one.
You could use the c function to combine the values into a vector:
a <- 1:10
a[-c(1,2,3)]
[1] 4 5 6 7 8 9 10
Related
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
say I have a spreadsheet that I imported or am working with a regular dataset. how do I retrieve a list of the top minimum values?
for example.
x<-c(6,7,8,9,10,1,2,3,4,5,11,12,13,14)
how do I get the position of the top 5 minimum values in the x vector (6,7,8,9,10)?
You can use order or sort.list to obtain the permutation of indices that can sort the object in ascending order (or descending if you pass descending = TRUE). Then you can obtain the necessary values using head or passing the indices ([1:5] in this example for the first five) directly
order(x)[1:5]
#[1] 6 7 8 9 10
sort.list(x)[1:5]
#[1] 6 7 8 9 10
We can use sort with index.return = TRUE
head(sort(x, index.return = TRUE)$ix,5)
Or with order
head(order(x),5)
#[1] 6 7 8 9 10
If I have a vector x, and I want to know which 5 values of x have the smallest values and their location in x.
The smallest 5 values of x will be sort(x)[1:5],
But how do I know what place these values have in the original x vector?
You are looking for the order function.
order returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments. sort.list is the same, using only one argument.
> x <- rnorm(10)
[1] 1.6722546 1.3608374 0.7912174 -0.7017244 -0.2093535 1.7224396 -0.9370661 -1.5226014 0.4416517 -0.0455294
> order(x)
[1] 8 7 4 5 10 9 3 2 1 6
> x[order(x)[1:3]]
[1] -1.5226014 -0.9370661 -0.7017244
Currently, this code works to do what I want to do where dx$res is a vector selecting values from dx$val1 or dx$val2 depending on value of dx$x0.
x0<-c(1,2,1,2,2,1)
val1<-c(8,6,4,5,3,2)
val2<-c(4,8,6,7,9,5)
dx<-data.frame(x0,val1,val2)
dx$res<-(dx$x0==1)*dx$val1+(dx$x0==2)*dx$val2
I would like to know if there were more elegant methods to do this like using apply function.
One option is model.matrix with rowSums. It is also more general for 'n' number of distinct elements in the 'x0' column.
dx$res <- rowSums(dx[-1]*model.matrix(~ factor(x0) - 1 , dx))
dx$res
#[1] 8 8 4 7 9 2
I've got a fairly basic question concerning vector operations in R. I want to apply a certain operation (i.e. increment) to specific elements of a vector by using a vector containing the indices of the elements.
For example:
ind <- c(2,5,8)
vec <- seq(1,10)
I want to add 1 to the 2nd, 5th and 8th element of vec. In the end I'd like to have:
vec <- c(1,3,3,4,6,6,7,9,8,10)
I tried vec[ind] + 1
but that returns only the three elements. I could use a for-loop, of course, but knowing R, I'm sure there's a more elegant way.
Any help would be much appreciated.
We have to assign it
vec[ind] <- vec[ind] + 1
vec
#[1] 1 3 3 4 6 6 7 9 9 10