[R] Data frame index?

Duncan Murdoch murdoch at stats.uwo.ca
Wed Jan 18 13:11:41 CET 2006


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




More information about the R-help mailing list