[R] Data type in a data frame
Rui Barradas
ruipbarradas at sapo.pt
Tue Oct 23 20:15:59 CEST 2012
Hello,
When read into a data.frame, R defaults to reading character strings as
factors. If you don't want that, use option stringsAsFactors = FALSE.
Using your dataset,
dat1 <- read.table(text = "
Observation Gender Dosage Alertness
1 m a 8
2 m a 12
3 m a 13
4 m a 12
5 m b 6
6 m b 7
", header = TRUE)
str(dat2)
dat2 <- read.table(text = "
Observation Gender Dosage Alertness
1 m a 8
2 m a 12
3 m a 13
4 m a 12
5 m b 6
6 m b 7
", header = TRUE, stringsAsFactors = FALSE)
str(dat2)
This is decided based on the setting of (which you can change)
options("stringsAsFactors")
Hope this helps,
Rui Barradas
Em 23-10-2012 15:43, asafwe escreveu:
> Hi all,
>
> How does R know to regard a variable as a factor and not a character?
> For example, consider the following table:
>
> Observation Gender Dosage
> Alertness
> 1 m a
> 8
> 2 m a
> 12
> 3 m a
> 13
> 4 m a
> 12
> 5 m b
> 6
> 6 m b
> 7
>
> When read into a dataframe, will "m", "a", "b" be regarded as a factor or as
> a character? How does R decide?
>
> Thanks a lot in advance,
>
> Asaf
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Data-type-in-a-data-frame-tp4647161.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
More information about the R-help
mailing list