Gundala Viswanath
2008-Jun-23 08:47 UTC
[R] Create a new vector with filled with mean value of original vector
Hi, Given this vector:> x <- c(30.9, 60.1 , 70.0 , 73.0 , 75.0 , 83.9 , 93.1 , 97.6 , 98.8 , 113.9)[1] 30.9 60.1 70.0 73.0 75.0 83.9 93.1 97.6 98.8 113.9> mean.x <- mean(x)[1] 79.63 I wish to: 1. Create a new vector (nx) with the same size as "x" 2. Fill "nx" with the mean value thus in the end I hope to get something like: [1] 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63 Is there a way to do it? - Gundala Viswanath Jakarta - Indonesia
Hans-Joerg Bibiko
2008-Jun-23 08:52 UTC
[R] Create a new vector with filled with mean value of original vector
On 23 Jun 2008, at 10:47, Gundala Viswanath wrote:> Hi, > > Given this vector: > >> x <- c(30.9, 60.1 , 70.0 , 73.0 , 75.0 , 83.9 , 93.1 , >> 97.6 , 98.8 , 113.9) > [1] 30.9 60.1 70.0 73.0 75.0 83.9 93.1 97.6 98.8 113.9 > >> mean.x <- mean(x) > [1] 79.63 > > I wish to: > > 1. Create a new vector (nx) with the same size as "x" > 2. Fill "nx" with the mean value > > thus in the end I hope to get something like: > > [1] 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63One way would be: rep(mean(x),length(x)) --Hans
Gavin Simpson
2008-Jun-23 09:05 UTC
[R] Create a new vector with filled with mean value of original vector
On Mon, 2008-06-23 at 17:47 +0900, Gundala Viswanath wrote:> Hi, > > Given this vector: > > > x <- c(30.9, 60.1 , 70.0 , 73.0 , 75.0 , 83.9 , 93.1 , 97.6 , 98.8 , 113.9) > [1] 30.9 60.1 70.0 73.0 75.0 83.9 93.1 97.6 98.8 113.9 > > > mean.x <- mean(x) > [1] 79.63 > > I wish to: > > 1. Create a new vector (nx) with the same size as "x" > 2. Fill "nx" with the mean value> rep(mean(x), length(x))[1] 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63 You have asked several introductory R questions today, suggesting you are a new useR. This basic material is covered in the documentation provided with R or available from the R Website. I suggest you start with 'An Introduction to R': http://cran.r-project.org/manuals.html The first 30 or so pages of which describe the major pieces of basic R usage. Far more efficient to consult the docs than to wait for a response from the list, even if the response time on R-Help is generally /very/ quick! HTH G> > thus in the end I hope to get something like: > > [1] 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63 79.63 > > Is there a way to do it? > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%