[R] Setting elements in data frame
Peter Dalgaard BSA
p.dalgaard at pubhealth.ku.dk
Thu Feb 22 14:17:35 CET 2001
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 replacement
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...)
> 2) Setting all elements == NA to -1000
> x[is.na(x)] <- -1000
Same 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list