Skip to content

Odd behaviour of R 2.00

6 messages · Hiroto Miyoshi, Brian Ripley, Peter Dalgaard

#
Dear R users

I have a data frame containing character and numeric variables, whose
name is seishin.  When I tried to assign NA to "" in the data frame, R, 2.00
showed an error message, such as
Error: NAs are not allowed in subscripted assignments

This did not happen under R 1.9.0.

More oddly,
The following commands work just fine under R 2.0.0
Why is this so?
And how can I assign NA to "" data.framewise in seishin data.frame?
Please help.

Sincerely
--------------------------------
Hiroto Miyoshi
h_m_ at po.harenet.ne.jp
#
"Hiroto Miyoshi" <h_m_ at po.harenet.ne.jp> writes:
It is the NA pattern on the left hand side that matters. Does it help
to use 

seishin[!is.na(seishin) & seishin==""]<-NA

?

(For atomic vectors you could also use %in% instead of ==, but this
doesn't work with data frames.)

  
    
#
Dear  Professor Dalgaard
Yes!  the above line worked perfectly.
But why? To me, !is.na(seishin) & seishin=="" seems redundant.
If you could explain this, it would be greatly appreciated.
Thank you.
--------------------------------
Hiroto Miyoshi
h_m_ at po.harenet.ne.jp
----- Original Message ----- 
From: "Peter Dalgaard" <p.dalgaard at biostat.ku.dk>
To: "Hiroto Miyoshi" <h_m_ at po.harenet.ne.jp>
Cc: <r-help at stat.math.ethz.ch>
Sent: Sunday, November 14, 2004 7:26 PM
Subject: Re: [R] Odd behaviour of R 2.00
2.00
http://www.R-project.org/posting-guide.html
#
On Sun, 14 Nov 2004, Hiroto Miyoshi wrote:

            
Without it, your logical matrix index contains NA.  What do you intend 
that to do?  Do you replace the corresponding element or not?  You don't 
know, so perhaps you set it to NA, whatever the rhs?  And do you use up a 
value on the rhs or not (not relevant if as here you are recycling a 
single value, except that you need to know how many times to recycle it)?

Prior to 2.0.0, R behaved inconsistently (both within itself and with S) 
with NA indices in assignments, so now we force the user to say what he 
intended.  This has picked up quite a number of errors.

I do think this would be cleaner and faster using a loop over columns, 
especially as you probably have factors in the data frame.  Read the code 
of "[<-.data.frame" if you don't see that.

  
    
#
"Hiroto Miyoshi" <h_m_ at po.harenet.ne.jp> writes:
It's due to this change (do check the NEWS file when things change
unexpectedly...):
 
    o   Subassignments involving NAs and with a replacement value of
        length > 1 are now disallowed.  (They were handled
        inconsistently in R < 2.0.0, see PR#7210.)  For data frames
        they are disallowed altogether, even for logical matrix indices
        (the only case which used to work).

Now, I'm getting slightly confused here, since there appears to be
exceptions:
`data.frame':   10 obs. of  2 variables:
 $ a: Factor w/ 10 levels "1","10","2","3",..: 1 3 4 5 6 NA 8 9 10 2
 $ b: Factor w/ 9 levels "","a","b","e",..: 2 3 1 NA 4 5 6 7 8 9
`data.frame':   10 obs. of  2 variables:
 $ a: Factor w/ 10 levels "1","10","2","3",..: 1 3 4 5 6 NA 8 9 10 2
 $ b: Factor w/ 9 levels "","a","b","e",..: 2 3 NA NA 4 5 6 7 8 9
_
platform i686-pc-linux-gnu
arch     i686
os       linux-gnu
system   i686, linux-gnu
status
major    2
minor    0.0
year     2004
month    10
day      04
language R

Also:
Error: NAs are not allowed in subscripted assignments

(difference being that 97 occurs in columns with NA's and 5 does not)
#
Dear Professor Ripley and Professor Dalgaard

Thank you for your quick reply.
Now, I understand.
Thank you.

--------------------------------
Hiroto Miyoshi
h_m_ at po.harenet.ne.jp
----- Original Message ----- 
From: "Prof Brian Ripley" <ripley at stats.ox.ac.uk>
To: "Hiroto Miyoshi" <h_m_ at po.harenet.ne.jp>
Cc: "Peter Dalgaard" <p.dalgaard at biostat.ku.dk>; <r-help at stat.math.ethz.ch>
Sent: Sunday, November 14, 2004 9:27 PM
Subject: Re: [R] Odd behaviour of R 2.00
--------------------------------
Hiroto Miyoshi
h_m_ at po.harenet.ne.jp
----- Original Message ----- 
From: "Peter Dalgaard" <p.dalgaard at biostat.ku.dk>
To: "Hiroto Miyoshi" <h_m_ at po.harenet.ne.jp>
Cc: "Peter Dalgaard" <p.dalgaard at biostat.ku.dk>;
<r-help at hypatia.math.ethz.ch>
Sent: Sunday, November 14, 2004 9:27 PM
Subject: Re: [R] Odd behaviour of R 2.00
R,
http://www.R-project.org/posting-guide.html