[R] double vs. list

David Andel
Fri, 12 Mar 2004 14:30:42 -0000

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