R Language - The average number of steps to get number - r

I have sequence of the numbers(Really it is just a piece of this sequence. In fact I have over 100k numbers)
1 2 3 3 2 3 2 3 2 1 2 3 2 3 2 3 3 2 3 2 3 2 1 3 3 2 3 3 2 3 3 3 2 3 2 3 2 1 3 2 3 3 3 2 3 3 2 3 2 3
I need to calculate the average number of steps after I get 1 in this sequence.
For example:
In this sequence 1 is first number. Now I count number of steps to get next 1 and I get 9. Next 1 is after 13 steps, next after 15 steps etc.
Now I have to calculate the average number of steps.
So there we have (9+13+15)/3= 12.(3)
How I can do this in R Language?

You can try:
mean(diff(which(x == 1)))
## [1] 12.33333
Given:
x <- c(1, 2, 3, 3, 2, 3, 2, 3, 2, 1, 2, 3, 2, 3, 2, 3, 3, 2, 3, 2,
3, 2, 1, 3, 3, 2, 3, 3, 2, 3, 3, 3, 2, 3, 2, 3, 2, 1, 3, 2, 3,
3, 3, 2, 3, 3, 2, 3, 2, 3)

Related

Create a vector with repeated values in DolphinDB

Given a vector, for example
a = [1, 2, 3]
How do I expand the vector to a new one like [1, 2, 3, 1, 2, 3, 1, 2, 3, ...] in DolphinDB?
You can use function take to take n values iteratively and sequentially and generate a new one.
a = [1, 2, 3]
take(a,9)
output
offset 0 1 2 3 4 5 6 7 8
0 1 2 3 1 2 3 1 2 3

R- dataframe with vector entries of different length

I have a vector that looks like this
[1] "NNNNNNNNNN" "NN NN NN NN NN" "NN NN NN NN NN" "N NN NNN NN NN"
[5] "NNNNNNNNNNNN" "NNN NNN NNN" "NN-NNNNNNN" "NNNNNNN"
[9] "NNNNNNN" "NNNNNNN"
The vector is coded as numbers further on in the code to look like this
[[1]]
[1] 2 2 2 2 2 2 2 2 2 2
[[2]]
[1] 2 2 9 2 2 9 2 2 9 2 2 9 2 2
[[3]]
[1] 2 2 9 2 2 9 2 2 9 2 2 9 2 2
[[4]]
[1] 2 9 2 2 9 2 2 2 9 2 2 9 2 2
[[5]]
[1] 2 2 2 2 2 2 2 2 2 2 2 2
[[6]]
[1] 2 2 2 9 2 2 2 9 2 2 2
It is actual this vector I need to create a dataframe from so I can transpose it and then use it in regression. When I try and use as.data.frame a get an error message say that lengths vary. Any help greatly appreciated
Here is the output of dput
dput(vecs2T)
list(c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2), c(2, 2, 9, 2, 2, 9, 2,
2, 9, 2, 2, 9, 2, 2), c(2, 2, 9, 2, 2, 9, 2, 2, 9, 2, 2, 9, 2,
2), c(2, 9, 2, 2, 9, 2, 2, 2, 9, 2, 2, 9, 2, 2), c(2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2), c(2, 2, 2, 9, 2, 2, 2, 9, 2, 2, 2),
c(2, 2, 9, 2, 2, 2, 2, 2, 2, 2), c(2, 2, 2, 2, 2, 2, 2),
c(2, 2, 2, 2, 2, 2, 2), c(2, 2, 2, 2, 2, 2, 2))
This is what I would like the data frame to look like
V1 V2 V3 V4 V5 V6 V7 V8
1 1 1 1 1 9 1 2 2
2 2 2 2 2 1 1 1 1
3 1 1 1 1 1 1 1 1
4 9 9 9 2 2 2 2 1
5 2 2 2 2 2 2 2 2
6 1 1 1 9 2 2 2 2
7 1 1 1 1 1 1 1 1
I would like the first number from each of the vectors to be under V1. So each row corresponds to one of the vectors in dput above. But because the vectors are of different length some of the columns will be empty which seem to be causing the trouble.

How to randomise a vector and keep the frequency of the elements fixed?

Extending this former question, how can I shuffle (randomize) the following vector
a1 = c(1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 5, 5)
in order to get something like this:
a2 = c(5, 5, 3, 3, 3, 3, 1, 1, 2, 4, 4, 4)
or even better like this:
a3 = c(4, 4, 4, 2, 3, 3, 3, 3, 1, 1, 5, 5)?
such that each element could randomly change to another but with keeping the number of each element constant?
You can try something like this: create a factor from a1 with randomly shuffled levels and then convert it to integers:
as.integer(factor(a1, levels = sample(unique(a1), length(unique(a1)))))
# [1] 5 5 4 4 4 4 3 3 2 1 1 1
The data:
a1 <- c(1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 5, 5)
First steps:
# extract values and their frequencies
val <- unique(a1)
tab <- table(a1)
freq <- tab[as.character(val)]
Keep original order of frequencies but sample values
rep(sample(val), freq)
# [1] 4 4 1 1 1 1 3 3 5 2 2 2
Keep original frequencies but sample order of values
rep(sa <- sample(val), freq[as.character(sa)])
# [1] 4 2 2 2 2 3 3 1 1 5 5 5
Seems like a perfect application for rle and its inverse rep:
rand_inverse_rle <- function(x) { x=sort(x)
ord=sample (length(rle(x)$values) )
unlist( mapply( rep, rle(x)$values[ord], rle(x)$lengths[ord]))}
rand_inverse_rle(a1)
#----------
[1] 3 3 4 5 5 5 2 2 2 2 1 1
This was my reading of a function needed to satisfy the natural language requirements:
> a1 = sample( c(1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 5, 5) )
> a1
[1] 5 2 5 2 5 1 3 4 2 2 3 1
> rand_inverse_rle(a1)
[1] 5 5 5 4 2 2 2 2 3 3 1 1
> rand_inverse_rle(a1)
[1] 1 1 3 3 5 5 5 2 2 2 2 4
> rand_inverse_rle(a1)
[1] 1 1 3 3 4 5 5 5 2 2 2 2

Moving a Vector (or other data object) from the RStudio Environment to an .R file

I want to capture data values from a post on SE into RStudio, and I manage to do so by copying the values, and then pasting them into the following command in the console:
> a = as.numeric(read.table(text = "8 8 4 1 2 2 0 2 5 2 3 3 3 1 5 4 4 1 4 2", sep = " "))
> a
[1] 8 8 4 1 2 2 0 2 5 2 3 3 3 1 5 4 4 1 4 2
Now a is in the global environment. The problem is that I would like to save it into an R file containing a number of other things, let's call it file.R, where vector a would appear as:
a <- c(8, 8, 4, 1, 2, 2, 0, 2, 5, 2, 3, 3, 3, 1, 5, 4, 4, 1, 4, 2)
Unfortunately for me, the only way I know is to type the commas manually. How can I do this otherwise?

Changing rows of data frame to columns in R

I have a data frame in following format.
Drug A 4 5 4 3 2 4 3 4 4
Drug B 6 8 4 5 4 6 5 8 6
Drug C 6 7 6 6 7 5 6 5 5
I want to convert it to following format without manually entering the value.
as by
pain = c(4, 5, 4, 3, 2, 4, 3, 4, 4, 6, 8, 4, 5, 4, 6, 5, 8, 6, 6, 7, 6, 6, 7, 5, 6, 5, 5)
drug = c(rep("A",9), rep("B",9), rep("C",9))
migraine = data.frame(pain,drug)
pain drug
1 4 A
2 5 A
3 4 A
4 3 A
5 2 A
6 4 A
...
25 6 C
26 5 C
27 5 C
Is there a better method to handle this?
I think this is an ideal use case for Hadley Wickham's reshape2 package. Here's a tutorial that will show you what you need. The melt function should do nicely for your purposes, I think.

Resources