Re: [R] Data frame index?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu 19 Jan 2006 - 01:10:52 EST

It's worth noting that there are quite a few for loops inside the code used by matrix indexing of data frames.

I think a single for-loop over the columns is as good as any, something like

DF <- data.frame(x=1, y=rep("a", 4), z = 3) ind <- c(1,3,3,1) # only numeric cols
for(i in unique(ind)) DF[ind==i, i] <- 0 DF

   x y z
1 0 a 3
2 1 a 0
3 1 a 0
4 0 a 3

On Wed, 18 Jan 2006, Duncan Murdoch wrote:

> On 1/18/2006 2:35 AM, Kenneth Cabrera wrote:
>> Hi, R users:
>>
>> I have a data.frame (not a matrix), I got a vector with the same length
>> as the
>> number of records (rows) of the data frame, and each element of
>> that vector is the column number (in a specific range of columns) of the
>> corresponding
>> record that I must set to zero.
>>
>> How can I do this without a "for" loop?
>
> It sounds as though you've found that you can use two-column matrix
> indexing on a data frame for reading but not assigning. You create a
> matrix where the first column is the row number, and the second column
> is the column number. Then indexing by that selects those particular
> elements in order.
>
> For instance, if you have named your vector of columns "cols", you'd do
>
> my.data.frame[ cbind(1:rows, cols) ] <- 0
>
> Here's an example:
>
> > df
> x y
> 1 1 a
> 2 1 a
> 3 1 a
> 4 1 a
> 5 1 a
> 6 1 a
> 7 1 a
> 8 1 a
> 9 1 a
> 10 1 a
> > df[cbind(1:4,c(1,2,1,2))]
> [1] "1" "a" "1" "a"
>
> But
>
> > df[cbind(1:4,c(1,2,1,2))] <- 0
> Error in "[<-.data.frame"(`*tmp*`, cbind(1:4, c(1, 2, 1, 2)), value = 0) :
> only logical matrix subscripts are allowed in replacement
>
> To get around this, construct the logical matrix using this method, then
> use it as an index:
>
> > mat <- matrix(FALSE, 10, 2)
> > mat[cbind(1:4,c(1,2,1,2))] <- TRUE
> > df[mat] <- 0
> Warning message:
> invalid factor level, NAs generated in: "[<-.factor"(`*tmp*`, thisvar,
> value = 0)
> > df
> x y
> 1 0 a
> 2 1 <NA>
> 3 0 a
> 4 1 <NA>
> 5 1 a
> 6 1 a
> 7 1 a
> 8 1 a
> 9 1 a
> 10 1 a
>
> If your columns are all numeric, you won't get the warning I got.
>
> Duncan Murdoch
>
> ______________________________________________
> 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
>

-- 
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 Thu Jan 19 02:31:40 2006

This archive was generated by hypermail 2.1.8 : Thu 19 Jan 2006 - 06:12:17 EST