This question already has answers here:
Calculate cumulative average (mean)
(7 answers)
Closed 5 years ago.
(I am sorry if the term is not correct).
In R, I have a numeric vector x. I want to create new vector y where:
y[i] = mean (x[1:i)
It is easy to write a function to calculate y, but is there a built-in function in R which do the task?
Thank you very much
Try this
y <- cumsum(x) / seq_along(x)
Reference
https://stat.ethz.ch/pipermail/r-help/2008-May/162729.html
Related
This question already has answers here:
How can i select values from a vector in R using logical operators?
(3 answers)
Closed 1 year ago.
I am running a simulation model and would like to know how to extract all values greater than 6 in this gamma distribution. Thank you!
cost <- 100
n_samp <-1000
gamma<-rgamma(n_samp,2,0.5)
You can also subset the array gamma with a logical vector:
gt6_values = gamma[gamma > 6]
You can use subset from base R to get just the values greater than 6.
subset(gamma, gamma > 6)
This question already has an answer here:
Inverse of Z-Normalize (z-score) Function on Matlab
(1 answer)
Closed 2 years ago.
If you have a data vector you are given that contains only z-scored variables is it possible to reverse the z-score values to get the original measure without using the original measure?
In R Program:
a = runif(100)
az = (a - mean(a))/sd(a)
Can you get back 'a' using JUST 'az'?
No, it is not possible. The reverse transformation requires knowledge of the summary statistics mean(a) and sd(a), which cannot be recovered from the standardized scores.
This question already has answers here:
Generate N random integers that sum to M in R
(3 answers)
Closed 7 years ago.
So this seems like a really simple question to me, but I can't seem to figure it out. I'm using R and I'm trying to generate a random sample, where the generated sample all sums to a set number.
Here's an approach to consider. Generate some random numbers:
n = 10
x <- runif(n) # or rnorm, rpois, whatever you want to use
And then scale() them to get the sum you want.
tot = 100 # this is the sum you want
x <- scale(x, center=FALSE, scale=sum(x)/tot)
all.equal(sum(x), tot) #TRUE
This question already has answers here:
3D equivalent of the curve function in R?
(4 answers)
Closed 8 years ago.
I have the following function in R:
n<- function(theta){
d=theta[1]
z=theta[2]
Nh= c(1819, 1018)
N= sum(Nh)
sigmah= c(0.013, 0.0155)
n=sum(Nh*sigmah)^2/(N^2*d^2/z^2+sum(Nh*sigmah^2))
return(n)
}
I would like to plot the function n over a range of inputs for d and z. How could I do this?
Thanks
I don't know what range of values is appropriate, but this produces something not too crazy:
library("emdbook")
curve3d(n(c(x,y)),xlim=c(1,2),ylim=c(1,2))
This question already has answers here:
Efficient calculation of matrix cumulative standard deviation in r
(2 answers)
Closed 9 years ago.
I'm trying to calculate the standard deviation of values in a time series, but I'd like to do it incrementally by advancing one day from the initial date value each time. I know there is a way to do this in R (probably using ddply?) that doesn't involve a nasty for-loop. Thanks for any help!
d<-seq(from=as.Date("2013-01-01"), to=as.Date("2013-02-01"), by="day")
v <-rnorm(32, 10, 5)
test.df<-data.frame(the_date=d, value=v)
Here's the way I'm doing it now.
result <- c()
for(i in 2:nrow(test.df)){ result[i-1] <- sd(test.df[1:i,]$value)}
Use TTR::runSD with cumulative=TRUE.
library(TTR)
x <- xts(test.df[,2],test.df[,1])
runSD(x, n=1, cumulative=TRUE)