> From: David Andel > > 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?
Use double brackets to access a list component. Single bracket gives you back a list with one component. I.e., try: x[1,][-y[[1]]] And BTW, isn't this really what you want? > apply(x, 1, function(x) x[x>=3]) [[1]] [1] 3 [[2]] [1] 5 7 HTH, Andy > > Thanks, > David > > ______________________________________________ > [EMAIL PROTECTED] mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > ------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}} ______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
