On Sep 26, 2012, at 2:27 PM, David Winsemius wrote:

> 
> On Sep 26, 2012, at 12:52 PM, JiangZhengyu wrote:
>> 
>> Hi everyone, I have a data frame Gene with SNPs eg.   P1 P2 P3 
>> CG CG GG
>> -- --  AC 
>> -- AC CC
>> AC  --  AC I tried to replace all the GG with a value 3.    
>> Gene[Gene=="GG"]<-3 It always give me:  Warning in `[<-.factor`(`*tmp*`, 
>> thisvar, value = 3) :
>> invalid factor level, NAs generated Does any know if there is anything wrong 
>> with my code?
> 
> You are trying to replace a factor value with a level that it doesn't have. 
> Hence that particular very informative error message.

You probably could do this:

Gene[] <- lapply(Gene, as.character)
> Gene
  P1 P2 P3
1 CG CG GG
2 -- -- AC
3 -- AC CC
4 AC -- AC

The use of the form `object[] <-` preserves the original dimensions. You would 
otherwise have needed to use data.frame() around the result.


> Gene[Gene=="GG"] <- 3
> Gene
  P1 P2 P3
1 CG CG  3
2 -- -- AC
3 -- AC CC
4 AC -- AC


> and provide commented, minimal, self-contained, reproducible code.

Your code was not really self-contained and reproducible, but what I did was 
this:

Gene <- read.table(text="P1 P2 P3 
 CG CG GG
 -- --  AC 
 -- AC CC
 AC  --  AC", header=TRUE)

read.table will by default create factors when the input column contains 
character values unless you use stringsAsFactors=FALSE.

-- 

David Winsemius, MD
Alameda, CA, USA

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to