Skip to content
Prev 27644 / 398498 Next

matrix subscripts in replacement

I'm reluctant to draw the S-PLUS and R comparison (these are different
programs after all), but could someone tell me why the following matrix
substitution works in S-PLUS, but not R.  I'm curious because matrix
substitution is a really slick way to "cleaning up" columns of data in data
frames.  For example, in the following I change values of 1 to values of 10,
but only for columns 1 and 3.

# S-PLUS example below
VAR1 VAR2 VAR3 
1    1    5    1
2    2    4    1
3    3    3    1
4    4    2    1
5    5    1   NA
VAR1 VAR2 VAR3 
1   10    5   10
2    2    4   10
3    3    3   10
4    4    2   10
5    5    1   NA

# R Example Below (version 1.6.1 Windows)
VAR1 VAR2 VAR3
1    1    5    1
2    2    4    1
3    3    3    1
4    4    2    1
5    5    1   NA
Error in "[<-.data.frame"(*tmp*, TEMP[, c(1, 3)] == 1 & !is.na(TEMP[,  : 
        matrix subscripts not allowed in replacement


Thanks!

Paul Bliese
Walter Reed Army Institute of Research