Re: [R] data frame returned from sapply but vector expected

2022-11-04 Thread PIKAL Petr
Hallo Ivan

Thanks, yes it seems to be working. I thought also removing NULL by

mylist2[sapply(mylist2, is.null)] <- NULL

but your approach is probably better (in any case simpler)

Thanks again.

Petr

> -Original Message-
> From: Ivan Krylov 
> Sent: Friday, November 4, 2022 1:37 PM
> To: PIKAL Petr 
> Cc: R-help Mailing List 
> Subject: Re: [R] data frame returned from sapply but vector expected
> 
> On Fri, 4 Nov 2022 15:30:27 +0300
> Ivan Krylov  wrote:
> 
> > sapply(mylist2, `[[`, 'b')
> 
> Wait, that would simplify the return value into a matrix when there are no
> NULLs. But lapply(mylist2, `[[`, 'b') should work in both cases, which in
my
> opinion goes to show the dangers of using simplifying functions in
to-be-library
> code.
> 
> Sorry for the double-post!
> 
> --
> Best regards,
> Ivan
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] data frame returned from sapply but vector expected

2022-11-04 Thread Ivan Krylov
On Fri, 4 Nov 2022 15:30:27 +0300
Ivan Krylov  wrote:

> sapply(mylist2, `[[`, 'b')

Wait, that would simplify the return value into a matrix when there are
no NULLs. But lapply(mylist2, `[[`, 'b') should work in both cases,
which in my opinion goes to show the dangers of using simplifying
functions in to-be-library code.

Sorry for the double-post!

-- 
Best regards,
Ivan

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] data frame returned from sapply but vector expected

2022-11-04 Thread Ivan Krylov
On Fri, 4 Nov 2022 12:19:09 +
PIKAL Petr  wrote:

> > 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

Is sapply(mylist2, `[[`, 'b') closer to what you'd like to see, i.e. a
list of vectors or NULLs?

-- 
Best regards,
Ivan

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] data frame returned from sapply but vector expected

2022-11-04 Thread PIKAL Petr
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
  petr.pi...@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/ | Information about
processing and protection of business partner's personal data are available
on website:

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/ |
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/

 

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.