Skip to content
Prev 154670 / 398506 Next

Avoiding factors and levels in data frames

Dear Ted,

I noticed that as.is was set by default in read.fwf. So if the user sets
stringsAsFactor it is passed through ... to read.table. But I'm not sure
how as.is is passed to read.table when onlye stringsAsFactors is set. If
it's the default (FALSE) then it might be conflicting with
stringsAsFactor. Therefore my suggestion to use as.is instead of
stringsAsFactor in this case.

I suppose it might be a good idea to add stringsAsFactor to the
argumentlist of read.fwf and give it the same defaults as read.table.

Cheers,

Thierry


------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Ted.Harding op manchester.ac.uk
Verzonden: maandag 1 september 2008 11:23
Aan: r-help op r-project.org
Onderwerp: Re: [R] Avoiding factors and levels in data frames
On 01-Sep-08 08:20:25, ONKELINX, Thierry wrote:
Can I ask for some elucidation about how the code operates here?
Apparently read.fwf() calls read.table(), and ?read.fwf refers
you to ?read.table for things like 'as.is' and 'stringsAsFactors'.

When I look at the code for read.table, I see in the paramater
list:

function (file, .... , as.is = !stringsAsFactors, ... ,
          stringsAsFactors = default.stringsAsFactors(), ... )

with *no further reference whatever* to 'stringsAsFactors' in the
body of the function. In particular, there is no test that I can
see of whether or not 'stringsAsFactors' has been set by the user
in the call.

The standard result of default.stringsAsFactors() is TRUE.

I've written a tiny test function:

  temp<-function(as.is = !stringsAsFactors,
        stringsAsFactors = default.stringsAsFactors()){
  print(c(as.is=as.is, sAF=stringsAsFactors))
  }

  temp()
# as.is   sAF
# FALSE  TRUE

  temp(stringsAsFactors = FALSE)
# as.is   sAF
#  TRUE FALSE

  temp(as.is=FALSE,stringsAsFactors = FALSE)
# as.is   sAF
# FALSE FALSE

So, if read.table is called with 'as.is=FALSE' (which is the default
set by read.fwf(), with any reference to 'stringsAsFactors' in the
call being part of the "..." which is passed to read.table()), then
read.table will be called with 'as.is=FALSE' regardless of whether
'stringsAsFactors=FALSE' has been set explicitly in calling read.fwf().

The only way to get 'as.is' to be TRUE would be to set it explicitly
in the call to read.fwf() (and in that case one need not bother with
'stringsAsFactors', since its only purpose seems to be to determine
the value of 'as.is'). Or, of course, to set default.stringsAsFactors
to be FALSE; but in many case people will want to have per-case
control over what happens in cases like this.

Well, that's how it seems to me, on reading the code. Is this what
Thierry really means when he says "stringsAsFactors is not set"?

If that is the case, then it seems to indicate some conflict or
inconsistency between read.fwf() and read.table() in this respect.
In any case, it strikes me as something of an undesirable tangle!

With thanks for any comments,
Ted.
[mailto:r-help-bounces op r-project.org]
raw1<-read.fwf(fn1,widths=widmax,col.names=headermax,stringsAsFactors=FA
INDICATES
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding op manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 01-Sep-08                                       Time: 10:22:55
------------------------------ XFMail ------------------------------

______________________________________________
R-help op 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.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in  this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document