This question already has answers here:
Calculating moving average
(17 answers)
Closed 3 years ago.
I have to calculate Simple Moving Averages on some data with this formula in R:
Does anybody know how to do that?
Are you looking for something like that?
Func <- function(xt){
St <- 1/10*sum(sapply(0:9, function(z) xt-z))
return(St)
}
And the output for two example of Xt:
> Func(10)
5.5
> Func(15)
10.5
Related
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
This question already has answers here:
Getting the last n elements of a vector. Is there a better way than using the length() function?
(6 answers)
Closed 5 years ago.
suppose I have daily time series data under variable name "prices", but im only interested in the past 100 days. How would i extract the last 100 elements from this variable?
Something equivalent to python's prices[-100:] but for R?
If it's a vector:
tail(prices, 100)
This question already has answers here:
Adding a column of means by group to original data [duplicate]
(4 answers)
Closed 6 years ago.
group=c("A","A","B","A","B","C","C","A")
y=c(3,4,5,2,1,4,1,2)
df=data.frame(group,y)
using aggregate, I can get the average by
aggregate(df$y, list(df$group), mean)
But my question is: How to do something like : (y_ij-mean_i)
where mean_i is the average for group i
thank you.
We can use ave
with(df, y- ave(y, group))
This question already has answers here:
Mean per group in a data.frame [duplicate]
(8 answers)
Closed 7 years ago.
I am trying to calculate mean salary for every job title from a data set which has 2159 job titles and convert into a list. My code
> for (i in 1:length(unique(sfs$JobTitle))) {
a<-print(paste((sfs$JobTitle[[i]])))
b<-print(paste((mean(sfs$BasePay[[i]]))))
ms<-list(a,b)
}
Also tried
for (i in 1:length(unique(sfs$JobTitle))) { ms<-matrix((sfs$JobTitle[[i]]),(mean(sfs$BasePay[[i]]))) }
The output I am getting is a list of 2 elements only. Can you guys help. Thanks
Perhaps you don't need a for loop. There are other ways to do it.
If you have a data.frame try this:
agg = aggregate(BasePay ~ JobTitle, data=sfs, mean)
This would work also:
sapply(split(sfs$BasePay, sfs$JobTitle), mean)
If you insist on having a list, use lapply:
lapply(split(sfs$BasePay, sfs$JobTitle), mean)
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))