Strings as factors

For all importing functions in theutilspackage, this argument isTRUE, which means that you import strings as factors. This only makes sense if the strings you import represent categorical variables in R. If you setstrings AsFactors to FALSE , the data frame columns corresponding to strings in your text file will be character.

# Import swimming_pools.csv correctly: pools

pools<-read.csv("swimming_pools.csv",stringsAsFactors=F)

# Check the structure of pools

str(pools)


Next to column names, you can also specify the column types or column classes of the resulting data frame. You can do this by setting thecolClassesargument to a vector of strings representing classes:

read.delim("my_file.txt",

colClasses = c("character",

"numeric",

"logical"))


# Previous call to import hotdogs.txt

hotdogs <- read.delim("hotdogs.txt", header = FALSE, col.names = c("type", "calories", "sodium"))

# Edit the colClasses argument to import the data correctly: hotdogs2

hotdogs2 <- read.delim("hotdogs.txt", header = FALSE,

col.names = c("type", "calories", "sodium"),

colClasses= c("factor", "NULL", "numeric"))

你可能感兴趣的:(Strings as factors)