Re: [R] nchar on data.frames

About this list Date view Thread view Subject view Author view

From: nels tomlinson (tomlinso@purdue.edu)
Date: Thu 29 Mar 2001 - 10:56:26 EST


Message-Id: <3AC2883A.8030909@purdue.edu>

Take a look at what you are taking the length of:

> temp <- list(text=c("thug","jimbob","apple","thug"),numbers=1:4)
>
> attributes(temp$text)
NULL
> attributes(temp)
$names
[1] "text" "numbers"

and then after you make it a data frame, they are very different things:

> temp<-data.frame(temp)
> attributes(temp)
$names
[1] "text" "numbers"

$row.names
[1] "1" "2" "3" "4"

$class
[1] "data.frame"

> attributes(temp$text)
$levels
[1] "apple" "jimbob" "thug"

$class
[1] "factor"

So, by making it a data.frame, you made the vector of strings into a
vector holding a factor (discrete-valued) variable with levels equal to
the three unique strings. I'll have to leave it to someone who knows
what he's doing to explain why the nchar() gives different lengths.

I think that that is a sensible transformation to make, by the way.
After all, if it is really data, that's probably what you really meant
... Of course, if what you really wanted was to make those character
strings into row names, use the row.name= argument of data.frame() to do
just that. If you REALLY wanted a vector of strings in there, use the
I() function to make it so. See the details section of the data.frame()
function for details on this. Here's how I made it work:

> temp <- data.frame(text=I(c("thug","jimbob","apple","thug")),numbers=1:4)
> attributes(temp)
$names
[1] "text" "numbers"

$row.names
[1] "1" "2" "3" "4"

$class
[1] "data.frame"

> attributes(temp$text)
$class
[1] "AsIs"

> nchar(temp$text)
[1] 4 6 5 4
>

By the way, when someone who's speaking of libre software says "read the
fine manual", he's usually speaking ironically; the manual usually isn't
"fine" ( but you must still read it!). R is wonderfully unique in that
the manual really is fine, even in comparison to the commercial
software. I've been looking through the documentation for S-Plus, which
is certainly the closest commercial approach to R, and I really don't
think that their documentation is superior to R's.

Hope this helps,
Nels

David L. Tabb wrote:

> I don't understand why nchar() gives different string lengths for vectors
> in a list than it does for vectors in a dataframe. Here's a snippet of
> code:
>
>> temp <- list(text=c("thug","jimbob","apple","thug"),numbers=1:4)
>> nchar(temp$text)
>
> [1] 4 6 5 4
>
>> temp <- data.frame(temp)
>> nchar(temp$text)
>
> [1] 1 1 1 1
>
> Could someone explain how I can get the former behavior for a text vector
> in a data.frame?
>
> Thanks! RPM reports I'm using R-base-1.2.1-2.
>
> Dave
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request@stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view

This archive was generated by hypermail 2b30 : Fri 22 Jun 2001 - 18:58:37 EST