[R] Avoiding factors and levels in data frames

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Mon Sep 1 10:20:25 CEST 2008


Try to add options(stringsAsFactors = FALSE) in your Rprofile.site (in
the etc directory). Using as.is = TRUE seems safer than stringsAsFactors
= FALSE in the read.fwf function. Because as.is is set to FALSE by
default and stringsAsFactors is not set.

HTH,

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 Asher Meir
Verzonden: zondag 31 augustus 2008 11:02
Aan: r-help op r-project.org
Onderwerp: [R] Avoiding factors and levels in data frames

Hello all.

I am an experienced R user, I have used R for many years for a wide
variety of applications. However, I keep on running into one obstacle:
I never want factors or levels in my data frames, but I keep on
getting them. Is there any way to globally turn this whole feature of
data frames off? Using options(stringAsFactors=FALSE) does not seem to
work.
Alternatively, if I have a data frame with levels, can I just get rid
of them in that data frame?

Here is an example: I have a large text file, of which part is in the
fixed-width tabular form I need. I created a widths vector and a
column names vector. I then read the file as follows:

raw1<-read.fwf(fn1,widths=widmax,col.names=headermax,stringsAsFactors=FA
LSE)

But raw1 still has factors! It is an old class data frame:

> is(raw1)
[1] "data.frame" "oldClass"

And it still has levels:
> raw1[1,1]
[1] Gustav wind
229 Levels: - - - - - - -     - - - - WIN       - - - M ... Z INDICATES
C

My question is:
1. Can I get rid of the levels in raw1?
2. Even better -- can I stop it getting read in as a data frame with
factors?
3. Even better -- can I just tell R to never use factors in my data
frames?

Or any other solution that occurs to people -- maybe this is the wrong
way to go about reading in fixed width data in this kind of file.

I would appreciate any help.

Asher

______________________________________________
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



More information about the R-help mailing list