Skip to content
Prev 308807 / 398503 Next

Data type in a data frame

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: