SV: [R] short way

Regin Reinert Regin.Reinert at gallup.dk.tnsofres.com
Tue Aug 29 15:08:29 CEST 2000


This should do what you want:

# construct a data.frame d

L3 <- LETTERS[1:3]
str(d <- data.frame(cbind(x=1, y=1:10), ch=sample(L3, 10, repl=TRUE)))
d

# replace y-value in d, where y=5
d$y[d$y==5] <- 100
d


Regin


-----Oprindelig meddelelse-----
Fra: Pesl Thomas [mailto:thomas.pesl at arcs.ac.at]
Sendt: 29. august 2000 14:02
Til: R-Help (E-Mail)
Emne: [R] short way


i want to replace certain values of a matrix.
i know how to replace them when the values are in a vector.
i tried the same way for a matrix and got this answer:

la[la==0]<-1
Error in [<-.data.frame(*tmp*, la == 0, value = 1) : 
        matrix subscripts not allowed in replacement

i found a way to do this with "for()",
but i think this is the worst way to do it.

So is there a better way?

    Thomas J. Pesl
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list