请参阅 read.csv 的帮助:?read.csv
。这是相关部分:
colClasses: character. A vector of classes to be assumed for the
columns. Recycled as necessary, or if the character vector
is named, unspecified values are taken to be ‘NA’.
Possible values are ‘NA’ (the default, when ‘type.convert’ is
used), ‘"NULL"’ (when the column is skipped), one of the
atomic vector classes (logical, integer, numeric, complex,
character, raw), or ‘"factor"’, ‘"Date"’ or ‘"POSIXct"’.
Otherwise there needs to be an ‘as’ method (from package
‘methods’) for conversion from ‘"character"’ to the specified
formal class.
Note that ‘colClasses’ is specified per column (not per
variable) and so includes the column of row names (if any).
祝你学习 R 顺利。这很困难,但在你通过前几个阶段后会很有趣(我承认这确实需要一些时间)。
尝试这个并相应地修复其他的:
ex <- read.csv("exampleshort.csv",header=TRUE,colClasses=c("integer","integer","factor","integer","numeric","factor","factor","integer","numeric","numeric","numeric","numeric"), na.strings=c("."))
正如 BenBolker 指出的那样,colClasses
争论可能是不需要的。但是,请注意,使用colClasses
参数可以使操作更快,尤其是对于大型数据集。
na.strings
必须指定。请参阅以下部分?read.csv
:
na.strings: a character vector of strings which are to be interpreted
as ‘NA’ values. Blank fields are also considered to be
missing values in logical, integer, numeric and complex
fields.
仅供参考(这不应该用作解决方案,因为最好的解决方案是一步正确导入数据):RET
未作为整数导入。它被导入为factor
。供将来参考,如果您想转换factor
to a numeric
, use
new_RET <-as.numeric(as.character(ex$RET))