[R] in continuation with the earlier R puzzle

Alain Guillet alain.guillet at uclouvain.be
Mon Jul 12 18:12:00 CEST 2010


  I don't know what is wrong with your code but I believe you should use 
ifelse instead of a for loop:

s <- ifelse(news1o > s2o, 1 , -1 )


Alain

On 12-Jul-10 16:09, Raghu wrote:
> When I just run a for loop it works. But if I am going to run a for loop
> every time for large vectors I might as well use C or any other language.
> The reason R is powerful is becasue it can handle large vectors without each
> element being manipulated? Please let me know where I am wrong.
>
> for(i in 1:length(news1o)){
> + if(news1o[i]>s2o[i])
> + s[i]<-1
> + else
> + s[i]<--1
> + }
>

-- 
Alain Guillet
Statistician and Computer Scientist

SMCS - IMMAQ - Université catholique de Louvain
Bureau c.316
Voie du Roman Pays, 20
B-1348 Louvain-la-Neuve
Belgium

tel: +32 10 47 30 50



More information about the R-help mailing list