I'm trying to assign NAs to values that satisfy certain conditions (more
complex than shown below) and it gives the right result, but breaks the loop
having done the first one viz:
new<-c(rep(5,4),6)
for (i in 1:6)
{new[new[i]>5.5][i]<-NA}
gives the correct result, though an error message appears which causes a
break if it's in a loop. If I can get rid of the error message and get the
loop to continue, this should work fine. I'm sure I'm missing a simple
solution, but can't seem to see it,
Any help, as always, greatly appreciated,
Paul
--
View this message in context:
http://n4.nabble.com/NAs-are-not-allowed-in-subscripted-assignments-tp1819094p1819094.html
Sent from the R help mailing list archive at Nabble.com.
Maybe you can withdraw the [i] in your code...
> for (i in 1:6)
+ {new[new[i]>5.5]<-NA}
> new
[1] 5 5 5 5 NA
Alain
On 09-Apr-10 11:23, Paul Chatfield wrote:> I'm trying to assign NAs to values that satisfy certain conditions
(more
> complex than shown below) and it gives the right result, but breaks the
loop
> having done the first one viz:
>
> new<-c(rep(5,4),6)
> for (i in 1:6)
> {new[new[i]>5.5][i]<-NA}
>
> gives the correct result, though an error message appears which causes a
> break if it's in a loop. If I can get rid of the error message and get
the
> loop to continue, this should work fine. I'm sure I'm missing a
simple
> solution, but can't seem to see it,
>
> Any help, as always, greatly appreciated,
>
> Paul
>
>
>
--
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
Sorry I forgot to add that you don't need the for loop: new[new>5.5] <- NA new [1] 5 5 5 5 NA Alain On 09-Apr-10 11:23, Paul Chatfield wrote:> new<-c(rep(5,4),6) > for (i in 1:6) > {new[new[i]>5.5][i]<-NA} >-- 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
Hi Paul, what's wrong with new[new>5.5]<-NA ? Btw. your variable new has a length of 5 not 6. hth. Am 09.04.2010 11:23, schrieb Paul Chatfield:> > I'm trying to assign NAs to values that satisfy certain conditions (more > complex than shown below) and it gives the right result, but breaks the loop > having done the first one viz: > > new<-c(rep(5,4),6) > for (i in 1:6) > {new[new[i]>5.5][i]<-NA} > > gives the correct result, though an error message appears which causes a > break if it's in a loop. If I can get rid of the error message and get the > loop to continue, this should work fine. I'm sure I'm missing a simple > solution, but can't seem to see it, > > Any help, as always, greatly appreciated, > > Paul > >-- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790