How to sort data by using loops? [closed] - r

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to write a function that rearranges a vector in ascending or descending order. I know I can use sort and order functions but I want to do it manually.

If you want to practice writing your own sorting function, here is a example which applies a recursion approach:
mysort <- function(v, descending = F) {
if (length(v)==1) return(v)
if (descending) return(c(max(v),mysort(v[-which.max(v)],descending = descending)))
return(c(min(v),mysort(v[-which.min(v)])))
}
EXAMPLE
v <- c(1,2,5,4,2,7)
# ascending manner
mysort(v)
# descending manner
mysort(v,descending = T)
such that
> mysort(v)
[1] 1 2 2 4 5 7
> mysort(v,descending = T)
[1] 7 5 4 2 2 1

Related

Create A Vector in R with size n+1 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to create an integer vector in R with size n+1, but R cant understand that with "n" i mean all the natural numbers.
Something like this:
n = 1:10
my_vector <- n+1
my_vector
[1] 2 3 4 5 6 7 8 9 10 11

R: function that returns a vector of unique integers with multiple occurrences [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Prompted:
Given a vector of integers, write a function that returns a vector of those unique integers with multiple occurrences and put the result in a data frame.
I do not know how to isolate integers with multiple occurrences. Perhaps using the unique function?
I guess I would then want to display the results with something like:
table()
as.data.frame(table())
Any help would be much appreciated!
> sample(1:10, 10, replace=TRUE) -> x
> x
[1] 5 3 2 10 10 5 9 5 5 6
> y <- rle(sort(x))
> y$values[y$lengths > 1]
[1] 5 10
> y$lengths[y$lengths > 1]
[1] 4 2
Or using table:
> table(x)[table(x) > 1]
x
5 10
4 2

how to get the count in r till it meet a specific value and continue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to write an R program that, given a vector, will return another vector giving the number of elements from each position in the input vector to the occurrence in the input vector of a specified value. For example, if the specified value is 0, then with:
input : x<-c(1,1,0,1,1,1,0)
desired output<- (2,1,0,3,2,1,0)
Thanks in advance :)
We can try
library(data.table)
ave(x, rleid(x), FUN= function(x) rev(seq_along(x)))*x
#[1] 2 1 0 3 2 1 0
We could also use rle from base R
rl <- inverse.rle(within.list(rle(x), values <- seq_along(values)))
ave(x, rl, FUN=function(x) rev(seq_along(x)))*x
#[1] 2 1 0 3 2 1 0

Filtering rows and counting columns - can Reduce or Filter do that? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table like so:
1 2 3 4 5
a b a c
b b
a a a a
c b c b
Is there a special syntax to use for either Filter or Reduce (or something else entirely?) to get it so only the rows with an 'a' (including blanks) are shown? Likewise, is there a built-in way to count the frequency of 'a' for each column or would I have to loop over those individually?
Can't think of a way to pass rows or columns to Reduce or Filter to achieve the first, although a data.frame might get passed in a column-wise fashion for the second question since it is a list of columns. apply is the usual mechanism for doing row-wise operations, but I can think of quicker methods. For the first, under the assumption it is named X and is either a matrix or a data.frame:
X[ rowSums(X=="a", na.rm-TRUE) > 0 , ]
For the second:
colSums( X == "a")

calculate peak values in a plot using R [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table with two variables.The data is from NMR.So when I plot I get a spectrum.I found the peaks in plot.But I need to know how to list the values of peak and store them into a variable.Anyone please help.
An easy implementation based on Brian Ripley's post at R-help:
peaks <- function(x, halfWindowSize) {
windowSize <- halfWindowSize * 2 + 1
windows <- embed(x, windowSize)
localMaxima <- max.col(windows, "first") == halfWindowSize + 1
return(c(rep(FALSE, halfWindowSize), localMaxima, rep(FALSE, halfWindowSize)))
}
Example:
x <- c(1,3,1,3,1)
peaks(x, 1)
## [1] FALSE TRUE FALSE TRUE FALSE

Resources