On 18/12/2010 8:34 AM, Luca Meyer wrote:> I am running this small program:
>
> x<- factor(c("A","B","A","C"))
> y<- c(1,2,3,4)
> w<-data.frame(x,y)
> if (w$x=="A"){
> w$z=1
> }
> w
> And I obtain:
>
> x y z
> 1 A 1 1
> 2 B 2 1
> 3 A 3 1
> 4 C 4 1
>
> And not
>
> x y z
> 1 A 1 1
> 2 B 2 NA
> 3 A 3 1
> 4 C 4 NA
>
> Like I should obtain. What am I doing wrong?
You're using "if", which only looks at the first element (as the
message
said). See ?ifelse for a conditional that applies to each vector entry.
Duncan Murdoch
>
> Please notice that I get a warning approximately saying - translated from
italian:
>
> In if (w$x == "A") { : the condition length> 1 and only the
first element will be used
>
> Thanks,
> Luca
> ______________________________________________
> R-help at 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.