[R] double vs. list

David Andel andel at ifi.unizh.ch
Fri Mar 12 15:30:42 CET 2004


Hi all

I've got trouble with getting a "list" object and not being able to 
change that:

Lets say x is a simple matrix and I want to remove all elements <3 to get
3
5 7

 > x <- matrix(c(1,2,3,5,1,7), nrow=2, ncol=3, byrow=T)
 > x
      [,1] [,2] [,3]
[1,]    1    2    3
[2,]    5    1    7
 > typeof(x)
[1] "double"
 > y <- sapply(c(1,2), function(i) which(x[i,]<3))
 > y
[[1]]
[1] 1 2

[[2]]
[1] 2

 > typeof(y)
[1] "list"

Of course I could remove these elements right away and produce a "x" 
without elements <3, but also of type "list".
But because rule will be more complicated than this I need to know the 
elements to remove explicitely first.

Now when trying to remove elements 1 and 2 from x[1,], I get an error:
 > y[1]
[[1]]
[1] 1 2

 > x[1,][-y[1]]
Error in -y[1] : Invalid argument to unary operator

But this works:

 > x[1,][-c(1,2)]
[1] 3

What can I do differently?


Thanks,
David




More information about the R-help mailing list