美文网首页
Strings as factors

Strings as factors

作者: 腾子_Lony | 来源:发表于2017-09-01 00:09 被阅读0次

    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

          本文链接:https://www.haomeiwen.com/subject/hbzudxtx.html