[R] lapply problem

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Fri Aug 13 13:47:55 CEST 2004


Hi Luis,

try to use,


lapply(z2, function(x) if(is.null(x)) c(0,0) else x)


I hope this helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Doctoral Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm



----- Original Message ----- 
From: "Luis Rideau Cruz" <Luisr at frs.fo>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, August 13, 2004 1:40 PM
Subject: [R] lapply problem


> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list