使用colClasses参数指定每列的类型.例如:
x <- read.csv("myfile.csv", colClasses=c("numeric","factor","character"))
Run Code Online (Sandbox Code Playgroud)
您可以指定列类.从 ?read.table
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).
Run Code Online (Sandbox Code Playgroud)
所以类似于:
types = c("numeric", "character", "factor")
read.table("file.txt", colClasses = types)
Run Code Online (Sandbox Code Playgroud)
应该做的伎俩.
就个人而言,我只是以字符串或因子的形式读取列,然后更改所需的列.
| 归档时间: |
|
| 查看次数: |
682 次 |
| 最近记录: |