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
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:
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:
backtransform `scale()` for plotting
(9 answers)
Closed 8 years ago.
I'm new in R. I would like to transform a set of numbers I have scaled using scale() to the original raw ones.
Here the code I used to scale the numbers
dataCluster <- dataFinal[, c(1)]
data_z <- as.data.frame(lapply(dataCluster, scale))
clusters <- kmeans (na.roughfix(data_z), 3)
where:
dataFinal is a data frame (3 columns x 100 rows)
clusters is a "data matrix" (3 columns x 3 rows).
I would like to create a clustersRaw with the raw values.
Can anyone help?
Don't know it this is going to solve, since you dind't provide your data. However:
#create a matrix 10x3
mat<-matrix(1:30,ncol=3)
#scale it
x<-scale(mat)
#restore it
t(t(x)*attr(x,"scaled:scale")+attr(x,"scaled:center"))
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)