[R] remove NaN from element in a vector in a list

2011-09-27 Thread Ben qant
Hello, What is the best way to turn a matrix into a list removing NaN's? I'm new to R... Start: mt = matrix(c(1,4,NaN,5,3,6),2,3) mt [,1] [,2] [,3] [1,]1 NaN3 [2,]456 Desired result: lst [[1]] [1] 1 3 [[2]] [1] 4 5 6 Thanks! Ben [[alternative HTML

Re: [R] remove NaN from element in a vector in a list

2011-09-27 Thread R. Michael Weylandt
Try this: alply(mt, 1, function(x) as.numeric(na.omit(x))) The as.numeric() addition may be necessary to strip the extra attributes na.omit() wants to add. Michael On Tue, Sep 27, 2011 at 4:02 PM, Ben qant ccqu...@gmail.com wrote: Hello, What is the best way to turn a matrix into a list

Re: [R] remove NaN from element in a vector in a list

2011-09-27 Thread R. Michael Weylandt
alply is from the plyr package. You'll need to call that if its not already loaded. M On Tue, Sep 27, 2011 at 4:07 PM, R. Michael Weylandt michael.weyla...@gmail.com wrote: Try this: alply(mt, 1, function(x) as.numeric(na.omit(x))) The as.numeric() addition may be necessary to strip the

Re: [R] remove NaN from element in a vector in a list

2011-09-27 Thread Ben qant
Excellent! Thank you! ben On Tue, Sep 27, 2011 at 2:07 PM, R. Michael Weylandt michael.weyla...@gmail.com wrote: alply is from the plyr package. You'll need to call that if its not already loaded. M On Tue, Sep 27, 2011 at 4:07 PM, R. Michael Weylandt michael.weyla...@gmail.com wrote:

Re: [R] remove NaN from element in a vector in a list

2011-09-27 Thread David Winsemius
On Sep 27, 2011, at 4:02 PM, Ben qant wrote: Hello, What is the best way to turn a matrix into a list removing NaN's? I'm new to R... Start: mt = matrix(c(1,4,NaN,5,3,6),2,3) mt [,1] [,2] [,3] [1,]1 NaN3 [2,]456 apply(mt, 1, function(x) x[!is.nan(x)] ) [[1]]

Re: [R] remove NaN from element in a vector in a list

2011-09-27 Thread Hadley Wickham
apply(mt, 1, function(x) x[!is.nan(x)] ) [[1]] [1] 1 3 [[2]] [1] 4 5 6 You need to be a little careful with apply: mt2 - matrix(c(1,4,2,5,3,6),2,3) apply(mt2, 1, function(x) x[!is.nan(x)] ) [,1] [,2] [1,]14 [2,]25 [3,]36 Depending on the input you will get a