Skip to content

nchar on data.frames

5 messages · David L. Tabb, Ben Bolker, nels tomlinson +1 more

#
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:
[1] 4 6 5 4
[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 at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Your character vector got turned into a factor (see ?data.frame).  One
way to track this kind of problem down is with the str() function.
For "read.table" there is an "as.is" parameter that will prevent this
happening, but it looks like for data.frame you have to do something like

data.frame(text=I(temp$text),numbers=temp$numbers))
On Wed, 28 Mar 2001, David L. Tabb wrote:

            

  
    
#
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:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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 at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Ben mentions using
%data.frame(text=I(temp$text),numbers=temp$numbers))

to read in text and numeric data in data.frame. I have read the help pages
about this, but not understood what was meant by protecting text data with
I. Does this mean that any text entry beginning with "I" will be read as
character string data variable rather than a factor?

Many thanks!

d 

S. David White
sdavidwhite at bigfoot.com
Columbus, Ohio
On Wed, 28 Mar 2001 ben at zoo.ufl.edu wrote:
%
%   Your character vector got turned into a factor (see ?data.frame).  One
%way to track this kind of problem down is with the str() function.
%For "read.table" there is an "as.is" parameter that will prevent this
%happening, but it looks like for data.frame you have to do something like
%
%
%On Wed, 28 Mar 2001, 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 at stat.math.ethz.ch
%> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
%>
%
%-- 
%318 Carr Hall                                bolker at zoo.ufl.edu
%Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
%Box 118525                                   (ph)  352-392-5697
%Gainesville, FL 32611-8525                   (fax) 352-392-3704
%
%-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
%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 at 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 at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Admittedly "protected by `I'" may not immediately conjure up the
particular construction shown below (although there is an example in the
help for data.frame() as well).  It doesn't mean that the text itself has
to begin with I, it means (as shown below) that the variable has to be
specified as I(x) rather than x itself.  This could be clumsy if for
example you wanted to convert a many-columned matrix to a data frame; I
guess you could do it with a loop (I just tried to figure out how to do it
with lapply() and a function levels(x)[x] which turns a factor back into a
text string, but it didn't work ...)

   Ben
On Wed, 28 Mar 2001, David White wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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 at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._