Re: [R] replacement in a dataframe

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed 26 Oct 2005 - 17:49:45 EST

On Wed, 26 Oct 2005, Marco Venanzi wrote:

> Hi,I want to replace some elements of a dataframe "a"J, using 2 index
vectors ("x" and "y") and a vector of replacement ("z") of the same length of "x" and "y".
> I've tried a[cbind(x,y)]<-z, but it doesn't work.How can I do it in a
> simply way?Thanks,

Only logical matrix indexing is supported for data frames: See ?"[.data.frame". So you need to create a logical matrix index. One way would be

aa <- is.na(a) # create a logical matrix of the same size aa[] <- FALSE
aa[cbind(x,y)] <- TRUE
a[aa] <- z

-- 
Brian D. Ripley,                  ripley@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

______________________________________________
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 Wed Oct 26 17:54:14 2005

This archive was generated by hypermail 2.1.8 : Wed 26 Oct 2005 - 21:15:10 EST