[R] character count

2008-12-12 Thread Ista Zahn
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]

> x
[1] 32 22 12

Any ideas?

__
R-help@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.


Re: [R] character count

2008-12-12 Thread Claudia Beleites

nchar (c("convert this to 47 because it has 47 characters", "this one has 26 
characters", "13 characters"))

HTH Claudia
-- 
Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali
Università degli Studi di Trieste
Via Alfonso Valerio 6/a
I-34127 Trieste

phone: +39 (0 40) 5 58-34 47
email: cbelei...@units.it

__
R-help@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.


Re: [R] character count

2008-12-12 Thread Ista Zahn
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  wrote:
> Hi
>
>
> r-help-boun...@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@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.
>
>

__
R-help@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.


Re: [R] character count

2008-12-12 Thread Gabor Grothendieck
On Fri, Dec 12, 2008 at 11:05 AM, Ista Zahn  wrote:
> 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))

The * is unnecessary.

>
> Thanks!
>
> On Fri, Dec 12, 2008 at 11:00 AM, Petr PIKAL  wrote:
>> Hi
>>
>>
>> r-help-boun...@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@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.
>>
>>
>
> __
> R-help@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.
>

__
R-help@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.


Re: [R] character count

2008-12-12 Thread Carlos Cuartas
Hi Ista,
one way could be:

ncharacters<-unlist(lapply(x,function(x)nchar(gsub(' ','',x
ncharacters




From: Ista Zahn 
To: r-help@r-project.org
Sent: Friday, December 12, 2008 10:31:10 AM
Subject: [R] character count

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]

> x
[1] 32 22 12

Any ideas?

__
R-help@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.



  
[[alternative HTML version deleted]]

__
R-help@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.