[R] data frame returned from sapply but vector expected
PIKAL Petr
petr@p|k@| @end|ng |rom prechez@@cz
Fri Nov 4 13:19:09 CET 2022
Hallo all
I found a strange problem for coding if part of list is NULL.
In this case, sapply result is ***list of data frames*** but if there is no
NULL leaf, the result is ***list of vectors***.
I tried simplify option but it did not help me neither I found anything in
help page.
The code is part of bigger project where I fill list by reading in data and
if it fails, the leaf is set to NULL. Then the the boxplot is created simply
by
boxplot(sapply(mylist2, "[", "b")) and the user is asked to select if values
should be rbinded or not.
Is it possible to perform some *apply without getting data frame as result
in case NULL leaf?
Here is an example (without boxplot)
df1 <- data.frame(a=rnorm(5), b=runif(5), c=rlnorm(5))
df2 <- data.frame(a=rnorm(5), b=runif(5), c=rlnorm(5))
mylist1 <- list(df1,df2, df3)
mylist2 <- list(NULL,df2, df3)
> str(sapply(mylist1, "[", "b"))
List of 3
$ b: num [1:5] 0.387 0.69 0.876 0.836 0.819
$ b: num [1:5] 0.01733 0.46055 0.19421 0.11609 0.00789
$ b: num [1:5] 0.593 0.478 0.299 0.185 0.847
> str(sapply(mylist2, "[", "b"))
List of 3
$ : NULL
$ :'data.frame': 5 obs. of 1 variable:
..$ b: num [1:5] 0.01733 0.46055 0.19421 0.11609 0.00789
$ :'data.frame': 5 obs. of 1 variable:
..$ b: num [1:5] 0.593 0.478 0.299 0.185 0.847
S pozdravem | Best Regards
RNDr. Petr PIKAL
Vedoucí Výzkumu a vývoje | Research Manager
PRECHEZA a.s.
nábř. Dr. Edvarda Beneše 1170/24 | 750 02 Přerov | Czech Republic
Tel: +420 581 252 256 | GSM: +420 724 008 364
<mailto:petr.pikal using precheza.cz> petr.pikal using precheza.cz |
<https://www.precheza.cz/> www.precheza.cz
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních
partnerů PRECHEZA a.s. jsou zveřejněny na:
<https://www.precheza.cz/zasady-ochrany-osobnich-udaju/>
https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about
processing and protection of business partner's personal data are available
on website:
<https://www.precheza.cz/en/personal-data-protection-principles/>
https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné
a podléhají tomuto právně závaznému prohlášení o vyloučení odpovědnosti:
<https://www.precheza.cz/01-dovetek/> https://www.precheza.cz/01-dovetek/ |
This email and any documents attached to it may be confidential and are
subject to the legally binding disclaimer:
<https://www.precheza.cz/en/01-disclaimer/>
https://www.precheza.cz/en/01-disclaimer/
More information about the R-help
mailing list