Message-ID: <f55e7cf50812120805t166733c4s8286efc3aae28855@mail.gmail.com>
Date: 2008-12-12T16:05:51Z
From: Ista Zahn
Subject: character count
In-Reply-To: <OF3F375F7C.7C2D179F-ONC125751D.005764E9-C125751D.0057EC45@precheza.cz>
Thanks to everyone who responded. This turns out to be amazingly easy.
To count characters including spaces:
nchar(x)
To count characters excluding spaces:
nchar(gsub(" *","",x))
Thanks!
On Fri, Dec 12, 2008 at 11:00 AM, Petr PIKAL <petr.pikal at precheza.cz> wrote:
> Hi
>
>
> r-help-bounces at r-project.org napsal dne 12.12.2008 16:31:10:
>
>> Dear list,
>> I have a variable that consists of typed responses. I wish to compute
>> a variable equal to the number of characters in the original variable.
>> For example:
>>
>> > x <- c("convert this to 32 because it has 32 characters", "this one
> has 22
>> characters", "12 characters")
>>
>> [Some magic function here]
>
> If you consider space as a character then
>
> nchar(x)
>
> gives you the result.
>
> If not so such construction can do it
>
> unlist(lapply(lapply(strsplit(x, " "), paste, collapse=""), nchar))
>
> Regards
> Petr
>
>>
>> > x
>> [1] 32 22 12
>>
>> Any ideas?
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
>
>