[R] replacing a factor value in a data frame

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Oct 28 17:36:59 CEST 2005


Federico Calboli <f.calboli at imperial.ac.uk> writes:

> Hi All,
> 
> I have the following problem, that's driving me mad.
> 
> I have a dataframe of factors, from a genetic scan of SNPs. I DO have
> NAs in the dataframe, which would look like:
> 
>    V4 V5 V6 V7   V8   V9 V10
> 1  TT GG TT AC   AG   AG  TT
> 2  AT CC TT AA   AA   AA  TT
> 3  AT CC TT AC   AA <NA>  TT
> 4  TT CC TT AA   AA   AA  TT
> 5  AT CG TT CC   AA   AA  TT
> 6  TT CC TT AA   AA   AA  TT
> 7  AT CC TT CC <NA> <NA>  TT
> 8  TT CC TT AC   AG   AG  TT
> 9  AT CC TT CC   AG <NA>  TT
> 10 TT CC TT CC   GG   GG  TT
> 
> 
> In the dataframe I have 1 column where one factor has been erroneosly
> given alternative readings: CG and GC. 
> 
> I want to change the instances of GC to CG and I use the code:
> 
> data[data[,30]=="GC", 30] = "CG"
> 
> but get the error:
> Error in "[<-.data.frame"(`*tmp*`, all[, 30] == "GC", 30
>         missing values are not allowed in subscripted as
> 
> Any hints?


data[isTRUE(data[,30]=="GC"), 30] = "CG"

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list