[R] Subassignments involving NAs in data frames

From: McGehee, Robert <Robert.McGehee_at_geodecapital.com>
Date: Fri 10 Jun 2005 - 04:10:22 EST


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@geodecapital.com

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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Jun 10 04:24:13 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:28 EST