is.null(mylist[1]) and is.null(mylist$a) returns different values

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

is.null(mylist[1]) and is.null(mylist$a) returns different values

zadig
Hi

I have a list :

mylist <- list( a = NULL, b = 1, c = 2 )

> mylist[1]
$a
NULL

> is.null(mylist[1])
[1] FALSE

> is.null(mylist$a)
[1] TRUE

why? I need to use mylist[1]
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is.null(mylist[1]) and is.null(mylist$a) returns different values

Huzefa Khalil
Hi,

Try

> is.null(mylist[[1]])
[1] TRUE

Notice the double square brackets.

From: ?`[`
"The most important distinction between [, [[ and $ is that the [ can
select more than one element whereas the other two select a single
element."

On Thu, Jun 15, 2017 at 11:33 AM, ce <[hidden email]> wrote:

> Hi
>
> I have a list :
>
> mylist <- list( a = NULL, b = 1, c = 2 )
>
>> mylist[1]
> $a
> NULL
>
>> is.null(mylist[1])
> [1] FALSE
>
>> is.null(mylist$a)
> [1] TRUE
>
> why? I need to use mylist[1]
> ______________________________________________
> [hidden email] 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.



--
Huzefa Khalil
PhD Candidate,
Department of Political Science,
University of Michigan

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is.null(mylist[1]) and is.null(mylist$a) returns different values

Rui Barradas
In reply to this post by zadig
Hello,

You have to be aware that mylist[1] and mylist[[1]] are different things.


class(mylist[1])
[1] "list"
class(mylist[[1]])
[1] "NULL"


Apparently you want/need the latter:

is.null(mylist[[1]])
[1] TRUE


Hope this helps,

Rui Barradas

Em 15-06-2017 16:33, ce escreveu:

> Hi
>
> I have a list :
>
> mylist <- list( a = NULL, b = 1, c = 2 )
>
>> mylist[1]
> $a
> NULL
>
>> is.null(mylist[1])
> [1] FALSE
>
>> is.null(mylist$a)
> [1] TRUE
>
> why? I need to use mylist[1]
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is.null(mylist[1]) and is.null(mylist$a) returns different values

Jeff Newmiller
In reply to this post by Huzefa Khalil
I find that the str function is more helpful for understanding the difference between a null list and a list containing a null list than the implicit print function call that the interpreter invokes when you enter an expression at the console.

str( mylist[1] )

--
Sent from my phone. Please excuse my brevity.

On June 15, 2017 8:39:47 AM PDT, Huzefa Khalil <[hidden email]> wrote:

>Hi,
>
>Try
>
>> is.null(mylist[[1]])
>[1] TRUE
>
>Notice the double square brackets.
>
>From: ?`[`
>"The most important distinction between [, [[ and $ is that the [ can
>select more than one element whereas the other two select a single
>element."
>
>On Thu, Jun 15, 2017 at 11:33 AM, ce <[hidden email]> wrote:
>> Hi
>>
>> I have a list :
>>
>> mylist <- list( a = NULL, b = 1, c = 2 )
>>
>>> mylist[1]
>> $a
>> NULL
>>
>>> is.null(mylist[1])
>> [1] FALSE
>>
>>> is.null(mylist$a)
>> [1] TRUE
>>
>> why? I need to use mylist[1]
>> ______________________________________________
>> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is.null(mylist[1]) and is.null(mylist$a) returns different values

Massoud Boroujerdi
In reply to this post by zadig
I think look at the manual

Is.null(mylist[[1]])

Will work

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of ce
Sent: 15 June 2017 16:33
To: [hidden email]
Subject: [R] is.null(mylist[1]) and is.null(mylist$a) returns different values

Hi

I have a list :

mylist <- list( a = NULL, b = 1, c = 2 )

> mylist[1]
$a
NULL

> is.null(mylist[1])
[1] FALSE

> is.null(mylist$a)
[1] TRUE

why? I need to use mylist[1]
______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is.null(mylist[1]) and is.null(mylist$a) returns different values

zadig
In reply to this post by Jeff Newmiller
Thank you all , very informative, never thought of doing a str( mylist[1] )  

-----Original Message-----
From: "Jeff Newmiller" [[hidden email]]
Date: 06/15/2017 11:56 AM
To: [hidden email], "Huzefa Khalil" <[hidden email]>, "ce" <[hidden email]>
Subject: Re: [R] is.null(mylist[1]) and is.null(mylist$a) returns different values

I find that the str function is more helpful for understanding the difference between a null list and a list containing a null list than the implicit print function call that the interpreter invokes when you enter an expression at the console.

str( mylist[1] )

--
Sent from my phone. Please excuse my brevity.

On June 15, 2017 8:39:47 AM PDT, Huzefa Khalil <[hidden email]> wrote:

>Hi,
>
>Try
>
>> is.null(mylist[[1]])
>[1] TRUE
>
>Notice the double square brackets.
>
>From: ?`[`
>"The most important distinction between [, [[ and $ is that the [ can
>select more than one element whereas the other two select a single
>element."
>
>On Thu, Jun 15, 2017 at 11:33 AM, ce <[hidden email]> wrote:
>> Hi
>>
>> I have a list :
>>
>> mylist <- list( a = NULL, b = 1, c = 2 )
>>
>>> mylist[1]
>> $a
>> NULL
>>
>>> is.null(mylist[1])
>> [1] FALSE
>>
>>> is.null(mylist$a)
>> [1] TRUE
>>
>> why? I need to use mylist[1]
>> ______________________________________________
>> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is.null(mylist[1]) and is.null(mylist$a) returns different values

R help mailing list-2
In reply to this post by Jeff Newmiller
By the way, NULL is not a "null list", it has class (and type and mode)
"NULL" not "list".   "NULL" is an odd class, in that it can have only one
instance and hence can never have any attributes.  "list" objects, like
other vector objects, may have length zero, but such objects are not the
same as NULL.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Jun 15, 2017 at 8:56 AM, Jeff Newmiller <[hidden email]>
wrote:

> I find that the str function is more helpful for understanding the
> difference between a null list and a list containing a null list than the
> implicit print function call that the interpreter invokes when you enter an
> expression at the console.
>
> str( mylist[1] )
>
> --
> Sent from my phone. Please excuse my brevity.
>
> On June 15, 2017 8:39:47 AM PDT, Huzefa Khalil <[hidden email]>
> wrote:
> >Hi,
> >
> >Try
> >
> >> is.null(mylist[[1]])
> >[1] TRUE
> >
> >Notice the double square brackets.
> >
> >From: ?`[`
> >"The most important distinction between [, [[ and $ is that the [ can
> >select more than one element whereas the other two select a single
> >element."
> >
> >On Thu, Jun 15, 2017 at 11:33 AM, ce <[hidden email]> wrote:
> >> Hi
> >>
> >> I have a list :
> >>
> >> mylist <- list( a = NULL, b = 1, c = 2 )
> >>
> >>> mylist[1]
> >> $a
> >> NULL
> >>
> >>> is.null(mylist[1])
> >> [1] FALSE
> >>
> >>> is.null(mylist$a)
> >> [1] TRUE
> >>
> >> why? I need to use mylist[1]
> >> ______________________________________________
> >> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.