[R] Setting elements in data frame

Christian Hoffmann christian.hoffmann at wsl.ch
Thu Feb 22 12:11:11 CET 2001


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list