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

zcatav zcatav at gmail.com
Wed Aug 3 15:54:12 CEST 2011


Gabor Grothendieck wrote:
> 
> 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))
> 
> 

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

This code results as follows. Data at [1,b] and [9,b] not managed as Date.
       a          b      c          d
1  58009      14915 0       <NA>
2 114761       <NA> 1 2008-11-05
3 184440       <NA> 1 2009-12-08
4 189372 2011-07-28 0       <NA>
5 105286 2011-07-28 0       <NA>
6 186717 2011-07-28 0       <NA>
7 189106 2011-07-28 0       <NA>
8 127306 2011-07-28 0       <NA>
9 157342      15089 0       <NA>


And the second code

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

results as follows. Data at [,b] are completly lost.

       a     b c          d
1  58009     1 0       <NA>
2 114761 14188 1 2008-11-05
3 184440 14586 1 2009-12-08
4 189372     1 0       <NA>
5 105286     1 0       <NA>
6 186717     1 0       <NA>
7 189106     1 0       <NA>
8 127306     1 0       <NA>
9 157342     1 0       <NA>

I think this solution not proper for me.

--
View this message in context: http://r.789695.n4.nabble.com/conditional-data-replace-recode-change-or-whatsoever-tp3714715p3715525.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list