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
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
I have a vector of length 1000. It contains (numeric) survey answers of 100 participants, thus 10 answers per participant. I would like to drop the first three values for every participant to create a new vector of length 700 (including only the answers to questions 4-10).
I only know how to extract every n-th value of the vector, but cannot figure how to solve the above problem.
vector <- seq(1,1000,1)
Expected output:
4 5 6 7 8 9 10 14 15 16 17 18 19 20 24 ...
Using a matrix to first structure and then flatten is one method. Another somewhat similar method is to use what I am calling a "logical pattern index":
head( # just showing the first couple of "segments"
vector[ c( rep(FALSE, 3), rep(TRUE, 10-3) ) ],
15)
[1] 4 5 6 7 8 9 10 14 15 16 17 18 19 20 24
This method can also be use inside the two argument version of [ to select rows ore columns using a logical pattern index. This works because of R's recycling of logical indices.
Thanks for providing example data, based on which this thread is reproducible. Here is one solution
c(matrix(vector, 10)[4:10, ])
We first convert the vector to a matrix with 10 rows, so that each column attributes to a participant. Then use row subsetting to remove first three rows. Finally the matrix is flattened to a vector again.
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
How can I extract the number of consecutive events without an empty event, from a vector?
I've tried rle() but it doesn't give me what I am looking for.
With this type of data
data=c(NA,NA,seq(1,4,0.5),NA,0,0,NA,NA,NA,NA,runif(10,-5,10),NA)
I want to be able to know the length of the different sequence of events without a NA. For this case the output should be: 7, 2 and 10.
I also need the positions of each sequence of events.
rle works here:
data=c(NA,NA,seq(1,4,0.5),NA,0,0,NA,NA,NA,NA,runif(10,-5,10),NA)
with(rle(!is.na(data)),lengths[values])
## [1] 7 2 10
You can get lengths and positions like this:
with(rle(!is.na(data)),cbind(length=lengths[values],position=cumsum(c(1,lengths))[values]))
## length position
## [1,] 7 3
## [2,] 2 11
## [3,] 10 17
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