I am trying to take a vector of numbers 5:0 and repeat it 3 times, every other time reversing its order - r

I'd think this would be simple using the rev() and seq() functions, but am struggling to get the reverse order part correct.
I'm trying to get 5432101234543210 from 5:0.

Not too hard to set as a function...
try_it <- function(x) {
c(rev(x), x[2:length(x-1)], rev(x)[2:length(x-1)])
}
try_it(0:5)
# [1] 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0
Edit
Extend function to have variable repeats
try_it <- function(x, reps) {
c(rev(x), rep(c(x[2:length(x-1)], rev(x)[2:length(x-1)]), (reps - 1) / 2))
}
try_it(0:5, 5)
# [1] 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0
Note: I've not worked hard to generalise this extension, it will not return the correct length for an even number of repetitions. I'm sure you could modify to suit your requirements.

Related

R - Occurence by variable

I have a dataset called restrictions and I know if people can do actions (eat with a fork, come out of bed...).
Each number represents with which level of difficulty each individual can do an action (1: No difficulty, 2: Some difficulties, 3: High difficulties, 4: Cannot do the action at all)
I am mostly interested in level 4.
The dataset looks like this (with many more variables)
> head(restrictions)
RATOI_I RAHAB_I RANOU_I RAELI_I RAACH_I RAREP_I RAMEN_I RAADM_I RAMED_I RADPI_I RADPE_I RABUS_I
1 4 4 1 1 4 4 4 4 1 1 4 4
2 4 3 3 1 4 4 4 4 4 2 4 4
I would like to know how many people are level 4 in RATOI_I (I can do that) and for these people level 4 in RATOI_I, how many are level 4 in RAHAB_I and each variable.
I looked at the function sapply() but I am completely lost, I do not know how to use it and with which function.
Or must I maybe use the group_by() function?
Thanks in advance!
You can use apply with sum using restrictions==4 to count the number equal 4 per column.
apply(restrictions==4, 2, sum)
#colSums(restrictions==4) #Alternative
#RATOI_I RAHAB_I RANOU_I RAELI_I RAACH_I RAREP_I RAMEN_I RAADM_I RAMED_I RADPI_I RADPE_I RABUS_I
# 2 1 0 0 2 2 2 2 1 0 2 2
Or only for those having restrictions$RATOI_I==4 (Thanks to #Daniel-o for pointing on this):
apply(restrictions[restrictions$RATOI_I==4]==4, 2, sum)
#colSums(restrictions[restrictions$RATOI_I==4]==4)
#RATOI_I RAHAB_I RANOU_I RAELI_I RAACH_I RAREP_I RAMEN_I RAADM_I RAMED_I RADPI_I RADPE_I RABUS_I
# 2 1 0 0 2 2 2 2 1 0 2 2
we can also do by base packages:
df[df<4]<-0
df[df==4]<-1
colSums(df)
>RATOI_I RAHAB_I RANOU_I RAELI_I RAACH_I RAREP_I RAMEN_I RAADM_I RAMED_I RADPI_I RADPE_I RABUS_I
2 1 0 0 2 2 2 2 1 0 2 2

runner:streak_run shows unexpected result when k remains unchanged

I'm using runner:streak_run to count sequences of 0 and 1 in a column called "inactive_indicator".
The column is= 0,0,0,1,1,1,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1
For runner::streak_run(inactive_indicator))
I get the following:
1,2,3,1,2,3,1,1,2,1,2,3,4,5,5,5,5,1,2,3,4
Why is it stuck on 5 when it should go up to 8?
In documentation it says that k - running window size. By default window size equals length(x). Allow varying window size specified by vector of length(x)
As I understand, the default definition should be enough.
Problem resolves and I get expected results when running:
runner::streak_run(inactive_indicator),k=length(inactive_indicator))
Why doesn't it work in the first place?
This can be solved with rle from base R
sequence(rle(inactive_indicator)$lengths)
#[1] 1 2 3 1 2 3 1 1 2 1 2 3 4 5 6 7 8 1 2 3 4
Checked with runner
runner::streak_run(inactive_indicator)
#[1] 1 2 3 1 2 3 1 1 2 1 2 3 4 5 6 7 8 1 2 3 4
It is possible that there are some leading/lagging spaces in the column and it is not numeric. In that case, use trimws
runner::streak_run(trimws(inactive_indicator))
data
inactive_indicator <- c(0,0,0,1,1,1,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1)

Patterned Vector in base

I'd like to produce a vector with the following repeating pattern:
1 1 2 1 2 3 1 2 3 4 ...
that ranges from one to some arbitrary stopping point.
I can hack it together using an sapply followed by an unlist, as in the following, but it sure feels like there should be a base call that is more direct than this.
repeating_function <- function(stop_point) {
res_list <- sapply(1:stop_point, FUN=function(x) {1:x}, simplify=TRUE)
res <- unlist(res_list)
return(res)
}
Which produces:
repeating_function(5)
[1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
An easier option would be
sequence(sequence(5))
#[1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Wrapping in a function
repeating_function(val) {
sequence(sequence(val))
}

R table function

If I have a vector numbers <- c(1,1,2,4,2,2,2,2,5,4,4,4), and I use 'table(numbers)', I get
names 1 2 4 5
counts 2 5 4 1
What if I want it to include 3 also or generally, all numbers from 1:max(numbers) even if they are not represented in numbers. Thus, how would I generate an output as such:
names 1 2 3 4 5
counts 2 5 0 4 1
If you want R to add up numbers that aren't there, you should create a factor and explicitly set the levels. table will return a count for each level.
table(factor(numbers, levels=1:max(numbers)))
# 1 2 3 4 5
# 2 5 0 4 1
For this particular example (positive integers), tabulate would also work:
numbers <- c(1,1,2,4,2,2,2,2,5,4,4,4)
tabulate(numbers)
# [1] 2 5 0 4 1

Adding elements to vector with initial element as maximum [duplicate]

Basically I want to generate a sequence, say:
n is 2, the sequence will be 112
n is 3, sequence is 112123
n is 5, sequence is 112123123412345
I did come up with a solution
n=5
seq=1
for (i in 2:n){
seq=c(seq,rep(1:n,len=i))
}
I am wondering if there is a way can do it without for loop?
Use sequence:
> sequence(1:5)
[1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Here is one possibility:
n<-5
unlist(lapply(1:n,function(x) 1:x))
## [1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
It'd do something like:
do.call('c', sapply(1:5, seq, from = 1))
# [1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
I misread the question as "how to generate that annoying puzzler sequence," which goes
1,11,21,1112,3112,... :-). So I figured I might as well write a solution to that.
puzseq<-function(seqlen) {
theseq<- list(1)
for( j in 2:seqlen) {
thetab<-table(theseq[[j-1]])
theseq[[j]]<-unlist( sapply( 1:length(thetab), function(k) c(thetab[k], as.numeric(names(thetab)[k])) ))
}
return(theseq)
}

Resources