[R] conditional data replace (recode, change or whatsoever)

Gabor Grothendieck ggrothendieck at gmail.com
Wed Aug 3 14:42:13 CEST 2011


On Wed, Aug 3, 2011 at 8:09 AM, zcatav <zcatav at gmail.com> wrote:
> Your suggestion works perfect as i pointed previous message. Now have another
> question about data editing. I try this code:
> X[X[,"c"]==1,"b"]<-X[,"d"]
> and results with error: `[<-.data.frame`(`*tmp*`, X[, "c"] == 1, "b", value
> = c(NA,  :
>  replacement has 9 rows, data has 2
>
> Logically i selected 2 rows with X[,"c"]==1. Than i want to replace in that
> rows its own data from "d" to "b" with X[,"b"]<-X[,"d"]. What is wrong?
>

Also check out transform and ifelse, e.g.

transform(X, b = ifelse(is.na(b) & c == 0, "2011-07-28", b))

transform(X, b = ifelse(c == 1, d, c))


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list