R - sequence with increasing interval - r

normal sequences with fixed intervals can be created using seq(from, to, by= )
is there a way to create a sequence with increasing intervals like the sequence below.
seq1 = c(2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192)
here each of vector element is 2^index

We can just use
## not `2 ^ 1:13`
2 ^ (1:13)
or
2 ^ seq(1, 13, 1)

Related

Generating a sequence where the gap between numbers increases

I need to generate a sequence on R where the gap between elements increases each time
Seq1:
1, 49, 100, 154, ... 19306
Seq2:
48, 99, 153, 210, ..., 19650
Note the gap between seq1 elements increases by 3 each time. 49-1 = 48, 100-49 =51, 154-100 = 54...
The gap between Seq2 elements also increases by 3 each time 99-48 =51, 153-99 = 54
Given the advice from #Dason:
seq1 <- seq(48, 19306,3)
which(cumsum(seq1) ==19650)
seq2 <- cumsum(seq1)[1:100]
seq3 <- seq(47, 19306, 3)
seq4 <- seq2 -seq3[1:100]

unique pairs or combinations from a vector

Where am I going wrong with my function.
I am trying to create a function which will count all the unique pairs in a vector, say I have the following input:
ar <- c(10, 20, 20, 30, 30, 30, 40, 50)
The number of unique pairs is 20 = 1, 30 = 1 so I can just sum these up and the total number of unique pairs is 2.
However everything I am trying is creating 30 as having 2 unique pairs (since 30 occurs 3 times in the vector.
n <- 9
ar <- c(10, 20, 20, 30, 30, 30, 40, 50)
CountThePairs <- function(n, ar){
for(i in 1:length(ar)){
sum = ar[i] - ar[]
pairs = length(which(sum == 0))
}
return(sum)
}
CountThePairs(n = NULL, ar)
Is there an easier way of doing this? I prefer the base R version but interested in package versions also.
Here's a simpler way using floor and table form base R -
ar <- c(10, 20, 20, 30, 30, 30, 40, 50)
sum(floor(table(ar)/2))
[1] 2
Example 2 - Adding one more 30 to vector so now there are 2 pairs of 30 -
ar <- c(10, 20, 20, 30, 30, 30, 30, 40, 50)
sum(floor(table(ar)/2))
[1] 3
If 2 30 pairs count as one "unique" pair then original solution by #tmfmnk was correct -
sum(table(ar) >= 2)
You could use sapply on the unique values of the vector to return a logical vector if that value is repeated. The sum of that logical value is then the number of unique pairs.
ar <- c(10, 20, 20, 30, 30, 30, 40, 50)
is_pair <- sapply(unique(ar), function(x) length(ar[ar == x]) > 1)
sum(is_pair)
#[1] 2
I'm not sure what behaviour you want if there are four 30's - does this count as one unique pair still or is it now two? If the latter, you would need a slightly different solution:
n_pair <- sapply(unique(ar), function(x) length(ar[ar == x]) %/% 2)
sum(n_pair)
#[1] 2

how to convert a numeric vector into a time vector

in can r I have a numeric vector like c(15, 270, 540, 30, 15, 1440) representing minutes - but how can I create a vector out of this displaying hours and minutes?
Thanks in advance
Jasmin
This uses no packages and gives hours and minutes as asked (as opposed to days, hours and minutes).
x <- c(15, 270, 540, 30, 15, 1440)
sprintf("%02d:%02d", x %/% 60, x %% 60)
## [1] "00:15" "04:30" "09:00" "00:30" "00:15" "24:00"

subsetting a range between two percentages (R)

I have a numeric vector consisting of 150 observations ranging from -217544 to 319842.
I would like to create a subset based on a range of percentages. In other words I would like to subset everything between 30% and 70%.
for instance:
bm.sort <- c(1, 2, 3, 4, 5, 6, 7, 8, 9 ,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
I would like for the code to select the middle 40% (7 to 14).
I have so far tried the code
bm.m <- subset(bm.sort, bm.sort >= quantile(bm.sort, 0.3 %between% bm.sort <= quantile(bm.sort, 0.7)))
and
bm.m <- subset(bm.sort, bm.sort >= quantile(bm.sort, 0.3 | bm.sort <= quantile(bm.sort, 0.7)))
However when I use this code I only receive one observation instead of the range of 60 observations that i should receive.
Any help would be appreciated.
I managed to figure it out (missing ")" ), sorry for taking up your time unnecessarily.
Thank you for all the help.

R: Average nearby elements in a vector

I have many vectors such as this: c(28, 30, 50, 55, 99, 102) and I would like to obtain a new vector where elements differing less than 10 from one to another are averaged. In this case, I would like to obtain c(29, 52.5, 100.5).
Another way
vec <- c(28, 30, 50, 55, 99, 102)
indx <- cumsum(c(0, diff(vec)) > 10)
tapply(vec, indx, mean)
# 0 1 2
# 29.0 52.5 100.5

Resources