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"))

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容