R - Weights? Proportion? About distribution - r

Sorry, I don't know how to express the title I want to ask.
Because I don't know what the "keyword" is, I can't find a solution.
my question is
Suppose I have a set of 10000 numbers, and its range is from 0~40000
```I use 10 groups as an example.
x <- data.frame(num=c(0,13,58,609,829,2574,6517,12257,16478,19841))
```
I hope he can redistribute a new set of numbers to him based on this data.frame number range.
Assuming a maximum of 20000, the range
1~2000=1, 2001~4000=2, 4001~6000=3, 6001~8000=4, 8001~10000=5,...,18001~20000=10
```
x <- data.frame(num=c(1,1,1,1,1,2,4,6,8,10))
```
Because the real situation is, I don't know what the maximum value is, so I need to find a kit to help.
If my instructions are not clear enough, please let me know
Thank you

We can use findInterval
findInterval(x$num, seq(2001, 20000, by = 2000)) + 1

Related

How to create a random walk in R that goes in different directions than -1 or +1?

Consider this two‐dimensional random walk:
where, Zt, Wt, t = 1,2,3, … are independent and identically distributed standard normal
random variables.
I am having problems in finding a way to simulate and plot the sample path of (X,Y) for t = 0,1, … ,100. I was given a sample:
The following code is an example of the way I am used to plot random walks in R:
set.seed(13579)
r<-sample(c(-1,1),size=100,replace=T,prob=c(0.5,0.5))
r<-c(10,r))
(w<-cumsum(r))
w<-as.ts(w)
plot(w,main="random walk")
I am not very sure of how to achieve this.
The problem I am having is that this kind of codes has a more "simple" result, with a line that goes either up or down, -1 or +1:
while the plot I need to create also goes from left to right and viceversa.
Would you help me in correcting the code I know so that it fits my task/suggesting a smarterst way to go about it? It would be greatly appreciated.
Cheers!
Instead of using sample, you need to use rnorm(100) to draw 100 samples from a standard normal distribution. Since the walk starts at [0, 0], we need to append a 0 at the start and do a cumsum on the result, i.e. cumsum(c(0, rnorm(100))).
We want to do this for both the x and y variables, then plot. The whole thing can be done in a single line of code in base R:
plot(x = cumsum(c(0, rnorm(100))), y = cumsum(c(0, rnorm(100))), type = 'l')

Interpolating blinks in eyetracking data - start/end intervals as time points

So, I apologise in advance for my poor attempt at explaining myself. I am rather lost.
Summary:
I am working with the eyelinker package in R to analyse pupil size data in a time-series fashion.
I have managed to create a set of intervals where blinks start and end (extendedBlinks, they extend 150 milliseconds each direction (1000Hz).
# Define set of intervals for blinks
Blk <- cbind(df$blinks$stime, df$blinks$etime)
# Extend blinks (100 milliseconds each way)
extendedBlinks <- Intervals(Blk) %>% expand(150, "absolute")
head(extendedBlinks)
output:
Object of class Intervals
6 intervals over R:
[4485724, 4486141]
[4485984, 4486657]
[4486549, 4486853]
[4486595, 4487040]
[4486800, 4489142]
[4498990, 4499339]
In my dataframe, I have PSL (Pupil Size Left), PSR (Pupil Size Right), and time (relative to the eyetracker, and has the same values as the intervals shown above.
So, I want to get the PSL/PSR (for the sake of the example, let's just stick to getting the PSL).
I've tried many things, nothing seems to work for me. I want to replace the given values in y1 with extendedBlinks[1,1] and extendedBlinks[1,2] respectively (and then iterate over the intervals to interpolate the blinks.
# Interpolation
x1 <- c(extendedBlinks[1,1],extendedBlinks[1,2])
y1 <- c(500, 550)
interp <- approx(x1,y1, n = extendedBlinks[1,2]-extendedBlinks[1,1])
plot(interp)
Again, sorry for the poorly worded question. I'll edit as I receive feedback to try and make it clearer.
Any ideas?
Cheers!

Re-classifying a random matrix

I am brand new to R and in some desperate need of help. I have created a random matrix and need to re-classify it. Each pixel is randomly generated from 0-255 and I need to able to classify the 0-255 digits into 8 classifications. How would I do this? Any help would be greatly appreciated and I have placed my code below. I know I could use a raster but I am unsure on how to use them.
Thanks
par(mar=rep(0,4))
m=matrix(runif(100),10,10)
image(m,axes=FALSE,col=grey(seq(0,1,length=255)))
I didn't think your example adequately fit your description of the problem (since runif only ranges from 0-1 if the limits are not specified) so I modified it to fit the natural language features:
m=matrix(runif(100, 0, 255),10,10)
m[] <- findInterval(m, seq(0, 256, length=8) )
image(m,axes=FALSE,col=grey(seq(0,1,length=255)))
The "[]" with no indices preserves the matrix structure of the m object. The findInterval function lets you do the same sort of binning as cut, but it returns a numeric vector rather than the factor that cut would give.

Simulating realistic noise for a calcium baseline

thanks to the truely amazing community my project group is one step closer to mimic realistic calcium baseline noise.
I simulated a typical calcium movement in a mathematical model:
Thanks to the community I could add random noise to the unrealistic baseline:
However, the noise dynamic is actually too fast. Is there a way to slow down the noise and create broader noise peaks instead of these spikes. I add an actual measurement to show you what I mean:
If this question is too specific, I apologize and will delete the post.
Best wishes and many thanks!
Please make your question and examples reproducible so that others can help. That being said, it looks like the baseline is a just a random normal -- probably created with something like x <- rnorm(500). One way to make this less jumpy is calculate a moving average. You could use a package like TTR or zoo to do this, or you can create your own function. For example:
x <- rnorm(500)
plot(x, type = "l")
ma <- function(x, n = 5){ filter(x, rep(1/n, n), sides = 2) }
plot(ma(x), type = "l")
plot(ma(x, 10), type = "l")
I see your point now. I have two suggestions for this case, maybe they will be of help :
Try to add noise to only a subset of your base line ( following is a 10%)
baseline.index = which(App[,2] == min(App[,2]))
baseline.index.subset = sample(x = baseline.index, size = 0.1 * length ( baseline.index) , replace = F)
noise = rnorm( length (baseline.index.subset))
App[ baseline.index.subset,2] = App[ baseline.index.subset,2] + noise
And try to play a bit with the mean and standard deviation of the noise. ie:
noise = rnorm( length (baseline.index.subset), mean = 0, sd = 0.1)
Let us know if this helps

Evenly distribute X values within a given range

This is driving me nuts. I am looking for the formula to evenly calculate x values within a range of minimum and maximum values. For instance...
min = 4
max = 20
x = 3
Should equate to...
= 8, 12, 16
I feel like the answer is right in front of my face, I'm sure I covered it at some point during one of my math or statistics courses, but I just can't puzzle it out. I have looked at similar questions here, but they are all programming specific and aren't really shedding any light for me. I would think there has to be a basic formula for this. Any help would be greatly appreciated.
assuming t = 1..x:
a[t] = min+t*(max-min) / (x+1)

Resources