使用 fread() 读取后无法子集数据框

Cannot subset data frame after reading with fread()

我正在尝试对名为 cars 的 table 进行子集化,如下所示。我不想在我的 subtable 中使用 Country 列,所以我使用 [,-1] 删除第一列,而是将我的新变量 cars.use 分配给 -1.这里发生了什么?

> library(data.table)
> cars <- fread('cars.csv', header = TRUE)
> typeof(cars)
[1] "list"
> head(cars)
Country                       Car  MPG Weight Drive_Ratio Horsepower Displacement Cylinders
1:    U.S.        Buick Estate Wagon 16.9  4.360        2.73        155          350         8
2:    U.S. Ford Country Squire Wagon 15.5  4.054        2.26        142          351         8
3:    U.S.        Chevy Malibu Wagon 19.2  3.605        2.56        125          267         8
4:    U.S.    Chrysler LeBaron Wagon 18.5  3.940        2.45        150          360         8
5:    U.S.                  Chevette 30.0  2.155        3.70         68           98         4
6:   Japan             Toyota Corona 27.5  2.560        3.05         95          134         4
> cars.use <- cars[,-1]
> cars.use
[1] -1

通过使用 fread,我们得到 data.table。要进行子集化,可以使用 data.tablewith=FALSE

cars[,-1, with=FALSE]

?data.table

中有描述

By default with=TRUE and j is evaluated within the frame of x; column names can be used as variables. When with=FALSE j is a character vector of column names or a numeric vector of column positions to select, and the value returned is always a data.table. with=FALSE is often useful in data.table to select columns dynamically.

数据

 cars <- data.table(Col1= 1:5, Col2= 6:10)

一个选项是将所有国家/地区列设置为 NULL。这可以按如下方式完成:

# Create dataframe
df <- read.delim(text='
Country Car MPG Weight Drive_Ratio Horsepower Displacement Cylinders
U.S. BuickEstateWagon 16.9 4.360 2.73 155 350 8
U.S. FordCountrySquireWagon 15.5 4.054 2.26 142 351 8
U.S. ChevyMalibuWagon 19.2 3.605 2.56 125 267 8
U.S. ChryslerLeBaronWagon 18.5 3.940 2.45 150 360 8
U.S. Chevette 30.0 2.155 3.70 68 98 4
Japan ToyotaCorona 27.5 2.560 3.05 95 134 4', sep=' ')

#> df
#  Country                    Car  MPG Weight Drive_Ratio Horsepower
#1    U.S.       BuickEstateWagon 16.9  4.360        2.73        155
#2    U.S. FordCountrySquireWagon 15.5  4.054        2.26        142
#3    U.S.       ChevyMalibuWagon 19.2  3.605        2.56        125
#4    U.S.   ChryslerLeBaronWagon 18.5  3.940        2.45        150
#5    U.S.               Chevette 30.0  2.155        3.70         68
#6   Japan           ToyotaCorona 27.5  2.560        3.05         95
#  Displacement Cylinders
#1          350         8
#2          351         8
#3          267         8
#4          360         8
#5           98         4
#6          134         4

# Remove the 'Country' columns from the dataframe 
df$Country <- NULL

#> df
#                     Car  MPG Weight Drive_Ratio Horsepower Displacement
#1       BuickEstateWagon 16.9  4.360        2.73        155          350
#2 FordCountrySquireWagon 15.5  4.054        2.26        142          351
#3       ChevyMalibuWagon 19.2  3.605        2.56        125          267
#4   ChryslerLeBaronWagon 18.5  3.940        2.45        150          360
#5               Chevette 30.0  2.155        3.70         68           98
#6           ToyotaCorona 27.5  2.560        3.05         95          134
#  Cylinders
#1         8
#2         8
#3         8
#4         8
#5         4
#6         4

您可以通过调用 fread().

来解决这个问题

如果您将 fread() 调用更改为按名称(或按编号)删除第一列,则在阅读时将跳过该列。

fread("cars.csv", drop = "Country", header = TRUE)

您遇到子集化问题的原因是因为 fread() return 默认情况下是一个数据 table。如果你需要一个数据 frame,将 data.table 参数更改为 FALSE.

cars <- fread("cars.csv", header = TRUE, data.table = FALSE)

现在我们有了一个数据框,您使用的代码 cars[,-1] 将起作用。如果要删除列和 return 数据框,请将这两者结合起来。

fread("cars.csv", drop = "Country", header = TRUE, data.table = FALSE)

有关详细信息,请参阅 help(fread)