Skip to content
Back to formatted view

Raw Message

Message-ID: <24889429-6DAA-4111-BB2D-025415569040@comcast.net>
Date: 2011-11-11T02:26:18Z
From: David Winsemius
Subject: Why does length("") == 1?
In-Reply-To: <CAG1tYwLgVaSRn21yAhvzQrk+bo4SzewNwpYKj1Ogqj_6tV7VYw@mail.gmail.com>

On Nov 10, 2011, at 7:36 PM, Worik R wrote:

> Thanks.
>
> Thinking like a C programmer again....

You are thinking of something else:

 > length(NULL)
[1] 0

>
> W
>
> On Fri, Nov 11, 2011 at 1:27 PM, Peter Langfelder <
> peter.langfelder at gmail.com> wrote:
>
>> On Thu, Nov 10, 2011 at 4:17 PM, Worik R <worikr at gmail.com> wrote:
>>> It seems obvious to me that the empty string "" is length 0.
>>
>> You are using the wrong function to find the length of a character  
>> string.
>>
>> length() returns the length of the object, and length of (non-NULL)
>> scalars is always 1. If you want the number of characters in a  
>> string,
>> use nchar():
>>
>>> nchar("")
>> [1] 0
>>
>> Peter


David Winsemius, MD
West Hartford, CT