[R] lapply problem

Luis Rideau Cruz Luisr at frs.fo
Fri Aug 13 13:40:24 CEST 2004


R-help,

I wish to replace NULL elements(or missing) in the following list :
> z2
$cod
    mean       sd 
62.56190 12.65452 

$haddock
    mean       sd 
36.61490 11.50365 

$ling
    mean       sd 
86.17949 20.43587 

$saithe
     mean        sd 
50.275847  5.453606 

$whiting
NULL

$"norway pout"
     mean        sd 
13.739623  1.393104 

$"great silver smelt"
     mean        sd 
25.600000  1.549193 

$"norway hadock"
     mean        sd 
24.685225  3.902989

If I use 
lapply(z2,function(x) ifelse(is.null(x),0,as.numeric(x))) 

I get 

> z2
$cod
[1] 62.5619

$haddock
[1] 36.6149

$ling
[1] 86.17949

$saithe
[1] 50.27585

$whiting
[1] 0

$"norway pout"
[1] 13.73962

$"great silver smelt"
[1] 25.6

$"norway hadock"
[1] 24.68522

i.e,only the first element in z2

And if I use

> z2<-lapply(msd.sumin.Z2S,function(x) ifelse(is.na(x),0,x))
Warning message: 
is.na() applied to non-(list or vector) in: is.na(x) 
> z2
$cod
    mean       sd 
62.56190 12.65452 

$haddock
    mean       sd 
36.61490 11.50365 

$ling
    mean       sd 
86.17949 20.43587 

$saithe
     mean        sd 
50.275847  5.453606 

$whiting
logical(0)

$"norway pout"
     mean        sd 
13.739623  1.393104 

$"great silver smelt"
     mean        sd 
25.600000  1.549193 

$"norway hadock"
     mean        sd 
24.685225  3.902989 



which cannot be coreced to data frame(which is my aim)

How can it be resolve??

Thank you


Luis Ridao Cruz
Fiskirannsóknarstovan
Nóatún 1
P.O. Box 3051
FR-110 Tórshavn
Faroe Islands
Phone:             +298 353900
Phone(direct): +298 353912
Mobile:             +298 580800
Fax:                 +298 353901
E-mail:              luisr at frs.fo
Web:                www.frs.fo




More information about the R-help mailing list