[R] Subassignments involving NAs in data frames

McGehee, Robert Robert.McGehee at geodecapital.com
Thu Jun 9 20:10:22 CEST 2005


I'm seeing some inconsistent behavior when re-assigning values in a data
frame. The first assignment turns all of the 0s in my data frame to 2s,
the second fails to do so. 

> df1 <- data.frame(a = c(NA, 0, 3, 4))
> df2 <- data.frame(a = c(NA, 0, 0, 4))
> df1[df1 == 0] <- 2 ## Works
> df2[df2 == 0] <- 2
Error: NAs are not allowed in subscripted assignments

Checking an old news file I see this:
    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).

which leaves me to believe that the assignment for both df1 and df2
should fail ("data frame ... disallowed altogether"), however that seems
not to be the case, since the example works for df1. Also, the
vectorized version works as expected (because the replacement value has
a length of 1).

> vec1 <- c(NA, 0, 3, 4)
> vec2 <- c(NA, 0, 0, 4)
> vec1[vec1 == 0] <- 2 ## Works
> vec2[vec2 == 0] <- 2 ## Also works

Is this behavior for data frames intentional? What's the best
alternative to df1[df1 == 0] <- 2 that doesn't fail in situations such
as df2? A simple loop over columns?

Best,
Robert

Robert McGehee
Quantitative Analyst
Geode Capital Management, LLC
53 State Street, 5th Floor | Boston, MA | 02109
Tel: 617/392-8396    Fax:617/476-6389
mailto:robert.mcgehee at geodecapital.com



This e-mail, and any attachments hereto, are intended for us...{{dropped}}




More information about the R-help mailing list