Hi all, I have a problem which I am biting my teeth into unsuccessfully: (x <- data.frame(S1=c("a","b","d","F"),N1=c(2,4,6,9),N2=c(6,NA,0,6))) S1 N1 N2 1 a 2 6 2 b 4 NA 3 d 6 0 4 F 9 6> is.na(x)S1 N1 N2 1 FALSE FALSE FALSE 2 FALSE FALSE TRUE 3 FALSE FALSE FALSE 4 FALSE FALSE FALSE No I want to be able to do: 1) Setting all elements == 6 to 6000 x[x == 6] <- 6000 Error in [<-.data.frame(*tmp*, x == 6, value = 6000) : matrix subscripts not allowed in replacement But just would solve the problem. So I tried to work with which():> (w <- which(x == 6,TRUE))row col 3 3 2 1 1 3 4 4 3 But x[w[,1],w[,2]] <- 6000;x S1 N1 N2 1 a 2 6000 2 b 4 NA 3 d 6000 6000 4 F 6000 6000 and not x[w[,1],w[,2]] <- 6000;x S1 N1 N2 1 a 6000 6000 2 b 4 NA 3 d 6000 4 F 9 6000 2) Setting all elements == NA to -1000 x[is.na(x)] <- -1000 What am I missing? Thanks for help, -christian Dr.sc.math.Christian W. Hoffmann Mathematics and Statistical Computing Landscape Modeling and Web Applications Swiss Federal Research Institute WSL Zuercherstrasse 111 CH-8903 Birmensdorf, Switzerland phone: ++41-1-739 22 77 fax: ++41-1-739 22 15 e-mail: Hoffmann at WSL.CH www: http://www.wsl.ch/staff/christian.hoffmann/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Christian Hoffmann <christian.hoffmann at wsl.ch> writes:> Hi all, > > I have a problem which I am biting my teeth into unsuccessfully: > > (x <- data.frame(S1=c("a","b","d","F"),N1=c(2,4,6,9),N2=c(6,NA,0,6))) > S1 N1 N2 > 1 a 2 6 > 2 b 4 NA > 3 d 6 0 > 4 F 9 6 > > is.na(x) > S1 N1 N2 > 1 FALSE FALSE FALSE > 2 FALSE FALSE TRUE > 3 FALSE FALSE FALSE > 4 FALSE FALSE FALSE > > No I want to be able to do: > 1) Setting all elements == 6 to 6000 > x[x == 6] <- 6000 > Error in [<-.data.frame(*tmp*, x == 6, value = 6000) : > matrix subscripts not allowed in replacementThis seems to work, save for a (sort of spurious) warning that 6000 is not a valid replacement value for the factor column: x[,]<-lapply(x,function(z){z[z==6]<-6000;z}) (Why the comma? Search me...)> 2) Setting all elements == NA to -1000 > x[is.na(x)] <- -1000Same story.. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 22 February 2001 12:11, Christian Hoffmann wrote:> Hi all, > > I have a problem which I am biting my teeth into unsuccessfully: > > (x <- data.frame(S1=c("a","b","d","F"),N1=c(2,4,6,9),N2=c(6,NA,0,6))) > S1 N1 N2 > 1 a 2 6 > 2 b 4 NA > 3 d 6 0 > 4 F 9 6 > > > is.na(x) > > S1 N1 N2 > 1 FALSE FALSE FALSE > 2 FALSE FALSE TRUE > 3 FALSE FALSE FALSE > 4 FALSE FALSE FALSE > > No I want to be able to do: > 1) Setting all elements == 6 to 6000Not elegant, nor fast, but works (old BASIC habits die hard): for (i in 1:nrow(x)) for (j in 1:ncol(x)) if (!is.na(x[i,j]) && x[i,j]==6) x[i,j] <- 6000 I'm sure there's a vectorized or otherwise much cleaner solution for this...> 2) Setting all elements == NA to -1000 > x[is.na(x)] <- -1000can be done equivalently.> > > What am I missing?The problem seems to be that you cannot do a matrix subscript (which you would do with [is.na(x)] ) on a data frame. This probably is reasonable, since it would only work for simple (matrix-like) data frames. You might try to work on the data frame column-wise, since columns have the same data.class Kaspar Pflugshaupt -- Kaspar Pflugshaupt Geobotanical Institute ETH Zurich, Switzerland http://www.geobot.umnw.ethz.ch mailto:pflugshaupt at geobot.umnw.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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thursday 22 February 2001 14:17, Peter Dalgaard BSA wrote:> This seems to work, save for a (sort of spurious) warning that 6000 is > not a valid replacement value for the factor column: > > x[,]<-lapply(x,function(z){z[z==6]<-6000;z}) > > (Why the comma? Search me...) >Works also without the comma: x <- data.frame(lapply(x,function(x) {x[x==6]<-6000;x})) but still gives the warning. Kaspar -- Kaspar Pflugshaupt Geobotanical Institute ETH Zurich, Switzerland http://www.geobot.umnw.ethz.ch mailto:pflugshaupt at geobot.umnw.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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hello, what about converting the data.frame to a matrix? x <- data.matrix(x) x[x==6] <- 6000 x <- as.data.frame(x) x S1 N1 N2 1 2 2 6000 2 3 4 NA 3 4 6000 0 4 1 9 6000 best, jan On Thu, 22 Feb 2001, Christian Hoffmann wrote:> Hi all, > > I have a problem which I am biting my teeth into unsuccessfully: > > (x <- data.frame(S1=c("a","b","d","F"),N1=c(2,4,6,9),N2=c(6,NA,0,6))) > S1 N1 N2 > 1 a 2 6 > 2 b 4 NA > 3 d 6 0 > 4 F 9 6 > > is.na(x) > S1 N1 N2 > 1 FALSE FALSE FALSE > 2 FALSE FALSE TRUE > 3 FALSE FALSE FALSE > 4 FALSE FALSE FALSE > > No I want to be able to do: > 1) Setting all elements == 6 to 6000 > x[x == 6] <- 6000 > Error in [<-.data.frame(*tmp*, x == 6, value = 6000) : > matrix subscripts not allowed in replacement-- +----------------------------------- Jan Goebel (mailto:jgoebel at diw.de) DIW Berlin Longitudinal Data and Microanalysis K?nigin-Luise-Str. 5 D-14195 Berlin -- Germany -- phone: 49 30 89789-377 +----------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._