[R] Change in how R handles assignments?
Prof Brian Ripley
ripley at stats.ox.ac.uk
Mon May 17 16:44:17 CEST 2004
Note, this is how R handles `replacement subsetting of data frames'.
On Mon, 17 May 2004, David Kreil wrote:
> I have been using the following code in earlier versions of R:
What `earlier versions'? As far as I know this was changed in 1.8.0.
You do not need to terminate lines in ;, BTW.
> > class(info)
>  "data.frame"
> > class(q)
>  "data.frame"
> > dim(q)
>  7488 68
> > dim(info)
>  12 8
> > dim(info[no,])
>  1 8
> The column names(info) did not exist in q before the assignment.
> What used to happen (as intended) was that they would be created, and each of
> the 7488 rows would be set the same value, info[no,].
But that was not what was documented. The intention is that the rhs be a
list, not a data frame.
> Now, in the current version of R this gives the error:
> Error in "[[<-.data.frame"(`*tmp*`, k, value = rep(value[[k]], len = n)) :
> replacement has 7488 rows, data has 1
> I can use the following as a workaround
> for (n in names(info))
> but it seems clunky. Is there a more elegant/simple way to do the same thing
> in the current version of R? It seems I'm unintentionally forcing it to do
> something it doesn't "like".
Here's a simple example that does the same in 1.8.0 as R-patched:
x <- data.frame(a=1:3, b=4:6, c=7:9)
info <- x[1:2]
x[, names(info)] <- info[1, ]
You can do
x[, names(info)] <- info[rep(1,3), ]
x[, names(info)] <- unclass(info[1, ])
We'll unclass a data frame rhs internally in future.
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help