nouse
nouse

Reputation: 3471

Removing Zeros in a list of vectors

I have a list of 36 vectors containing sorted integers, including lots of zeros.

List of 36
 $ R11: int [1:361] 241 240 239 349 238 237 236 235 234 233 ...
 $ R12: int [1:361] 287 286 285 350 284 283 282 281 280 279 ...

I do not know how remove the zeros from the vectors. I tried lapply (list, list(list x!=0), but this wasnt successful. Thank you.

Upvotes: 1

Views: 22227

Answers (2)

Paul Lasky
Paul Lasky

Reputation: 41

Quick and easy zero remover is: if(length(which(x==0)!=0)) x[-which(x==0)]

Upvotes: 4

ekstroem
ekstroem

Reputation: 6191

If l is your list then

lapply(l, function(x) {x[x!=0]})

might work (but you can end up with list elements of different lengths). Calling your list list might not be the best option

Upvotes: 7

Related Questions