Skip to content

replacing values in a vector

11 messages · Sven Garbade, Jens Nieschulze, Kaspar Pflugshaupt +7 more

#
Hi all,

there is a vector v with several NAs. I want to create a new vector n of
the same length as v and the same NAs as in v and tried this:

n <- vector(length=length(v), mode="numeric")
replace(n, which(is.na(v)), NA)

but this does't work, all values in n are 0. What went wrong?

Thanks, Sven
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Thu, 26 Jul 2001, Sven Garbade wrote:

            
function (x, list, values) 
{
    x[list] <- values
    x
}
from the function body I gues you should use
instead because I assume that x is a local variable
so the global n wouldn't be changed by replace()

	J
***********************************************************************
Jens Nieschulze

Institute for Forest Biometrics &	Phone: ++49-551-39-12107
Applied Computer Science		Fax  : ++49-551-39-3465
Buesgenweg 4
37077 Goettingen		E-mail: jniesch at uni-forst.gwdg.de
GERMANY				http://www.uni-forst.gwdg.de/~jniesch

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Thursday 26 July 2001 13:42, Sven Garbade wrote:

            
You have to assign the result of the replacement, like in

n <- replace(n, which(is.na(v)), NA)

Or, somewhat simpler:

n[is.na(v)] <- NA


Cheers

Kaspar Pflugshaupt
#
Jens Nieschulze wrote:
oh, of course... Thanks!

Sven
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
n <- rep(0, length(v)) #Or replace 0 by the initial value you want in the
vector n
n[is.na(v)] <- NA

Best Regards,

Philippe


..........]<(({?<...............<?}))><...............................
 ) ) ) ) )	 __               	 __
( ( ( ( ( 	|__)              	|  _
 ) ) ) ) )	|   hilippe       	|__)rosjean
( ( ( ( ( 	Marine Biol. Lab., ULB, Belgium
 ) ) ) ) )	                  	 __
( ( ( ( ( 	|\  /|            	|__)
 ) ) ) ) )	| \/ |ariculture &	|__)iostatistics
( ( ( ( (
 ) ) ) ) )	e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org
( ( ( ( ( 	SciViews project coordinator (http://www.sciviews.org)
 ) ) ) ) )      tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home)
( ( ( ( (
 ) ) ) ) )      "I'm 100% confident that p is between 0 and 1"
( ( ( ( (                                  L. Gonick & W. Smith (1993)
 ) ) ) ) )
.......................................................................


-----Message d'origine-----
De : owner-r-help at stat.math.ethz.ch
[mailto:owner-r-help at stat.math.ethz.ch]De la part de Sven Garbade
Envoye : jeudi 26 juillet 2001 13:43
A : R-Help
Objet : [R] replacing values in a vector


Hi all,

there is a vector v with several NAs. I want to create a new vector n of
the same length as v and the same NAs as in v and tried this:

n <- vector(length=length(v), mode="numeric")
replace(n, which(is.na(v)), NA)

but this does't work, all values in n are 0. What went wrong?

Thanks, Sven
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
You should assign the result of replace back to n:

n <- replace(n, which(is.na(v)), NA)

Giovanni
#
Actually, if you just want a vector as v
with 0 except for the NA, the easiest is just:

v*0

which will let be the NA if present.

Agus


Dr. Agustin Lobo
Instituto de Ciencias de la Tierra (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona SPAIN
tel 34 93409 5410
fax 34 93411 0012
alobo at ija.csic.es



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Note that the below does not work for Inf/-Inf (gives NaN)

Giles

Dr. G.T. Innocent
Comparative Epidemiology and Informatics Group,
Dept. of Veterinary Clinical Studies,
Glasgow University Vet. School.
Bearsden Road, Glasgow G61 1QH
Tel. 0141 339 8855 Ext. 0531.
Fax. 0141 330 5729
e-mail G.Innocent at vet.gla.ac.uk
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
I have a dataset (of say values of X), each value associated
with a level of a factor F (25 levels in all), in a dataframe.

I want to make a single plot which contains 25 histograms of X,
one for each level of F and each one appropriately labelled.

(And, since there are 25 levels, a 5x5 array would be nice).

What's the trick in R, please?

With thanks,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 27-Jul-01                                       Time: 20:47:11
------------------------------ XFMail ------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Fri, 27 Jul 2001 Ted.Harding at nessie.mcc.ac.uk wrote:

            
Use the histogram function in the new 'lattice' package

	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
2 days later
#
Ted.Harding at nessie.mcc.ac.uk writes:
The trick is to the par() function. Here is an example ...

        #
        # generate some dummy data
        #
        x <- rnorm(n = 1000, mean = 12, sd = 1)
        f <- round(runif(n = 1000, min = 1, max = 25))

        #
        # set device for 5 * 5 array of charts
        #
        par(mfrow = c(5,5))

        #
        # plot histograms
        #
        tapply(x, f, hist, ylab="n", xlab="x")

I think you'll need a for() loop to get better control over the plots.
Something like:

        for(i in min(f):max(f))
          {
          hist(x[f==i], xlim=range(x), ylab="n", xlab="x", main = i)
          }

But I might be wrong.

I hope that helps.

Mark



--
Mark Myatt


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._