仅读取有限数量的列

Sta*_*Cub 124 import r r-faq

任何人都可以告诉我如何只阅读下面数据的前6个月(7列),例如使用read.table()

Year   Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec   
2009   -41  -27  -25  -31  -31  -39  -25  -15  -30  -27  -21  -25
2010   -41  -27  -25  -31  -31  -39  -25  -15  -30  -27  -21  -25 
2011   -21  -27   -2   -6  -10  -32  -13  -12  -27  -30  -38  -29
Run Code Online (Sandbox Code Playgroud)

Rei*_*son 156

假设数据在文件中data.txt,您可以使用跳过列的colClasses参数read.table().这里前7列中的数据是"integer",我们将剩下的6列设置为"NULL"表示应该跳过它们

> read.table("data.txt", colClasses = c(rep("integer", 7), rep("NULL", 6)), 
+            header = TRUE)
  Year Jan Feb Mar Apr May Jun
1 2009 -41 -27 -25 -31 -31 -39
2 2010 -41 -27 -25 -31 -31 -39
3 2011 -21 -27  -2  -6 -10 -32
Run Code Online (Sandbox Code Playgroud)

根据实际数据"integer"类型更改为详细信息中的一种可接受类型?read.table.

data.txt 看起来像这样:

$ cat data.txt 
"Year" "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
2009 -41 -27 -25 -31 -31 -39 -25 -15 -30 -27 -21 -25
2010 -41 -27 -25 -31 -31 -39 -25 -15 -30 -27 -21 -25
2011 -21 -27 -2 -6 -10 -32 -13 -12 -27 -30 -38 -29
Run Code Online (Sandbox Code Playgroud)

并通过使用创建

write.table(dat, file = "data.txt", row.names = FALSE)
Run Code Online (Sandbox Code Playgroud)

这里dat

dat <- structure(list(Year = 2009:2011, Jan = c(-41L, -41L, -21L), Feb = c(-27L, 
-27L, -27L), Mar = c(-25L, -25L, -2L), Apr = c(-31L, -31L, -6L
), May = c(-31L, -31L, -10L), Jun = c(-39L, -39L, -32L), Jul = c(-25L, 
-25L, -13L), Aug = c(-15L, -15L, -12L), Sep = c(-30L, -30L, -27L
), Oct = c(-27L, -27L, -30L), Nov = c(-21L, -21L, -38L), Dec = c(-25L, 
-25L, -29L)), .Names = c("Year", "Jan", "Feb", "Mar", "Apr", 
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = "data.frame",
row.names = c(NA, -3L))
Run Code Online (Sandbox Code Playgroud)

如果事先不知道列数,则效用函数count.fields将读取文件并计算每行中的字段数.

## returns a vector equal to the number of lines in the file
count.fields("data.txt", sep = "\t")
## returns the maximum to set colClasses
max(count.fields("data.txt", sep = "\t"))
Run Code Online (Sandbox Code Playgroud)

  • @Andrie`财富(109)` (3认同)

Jaa*_*aap 67

要从数据集中读取特定的一组列,还有其他几个选项:

1)fread来自data.table-package:

您可以使用指定所需的列select从参数freaddata.table包.您可以使用列名称或列号向量指定列.

对于示例数据集:

library(data.table)
dat <- fread("data.txt", select = c("Year","Jan","Feb","Mar","Apr","May","Jun"))
dat <- fread("data.txt", select = c(1:7))
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用该drop参数指示不应读取哪些列:

dat <- fread("data.txt", drop = c("Jul","Aug","Sep","Oct","Nov","Dec"))
dat <- fread("data.txt", drop = c(8:13))
Run Code Online (Sandbox Code Playgroud)

所有结果都是:

> data
  Year Jan Feb Mar Apr May Jun
1 2009 -41 -27 -25 -31 -31 -39
2 2010 -41 -27 -25 -31 -31 -39
3 2011 -21 -27  -2  -6 -10 -32
Run Code Online (Sandbox Code Playgroud)

更新:如果您不想fread返回data.table,请使用data.table = FALSE-parameter,例如:fread("data.txt", select = c(1:7), data.table = FALSE)

2)read.csv.sql来自sqldf-package:

另一个替代方案是包中的read.csv.sql功能sqldf:

library(sqldf)
dat <- read.csv.sql("data.txt",
                    sql = "select Year,Jan,Feb,Mar,Apr,May,Jun from file",
                    sep = "\t")
Run Code Online (Sandbox Code Playgroud)

3)使用read_*-package中的readr-functions:

library(readr)
dat <- read_table("data.txt",
                  col_types = cols_only(Year = 'i', Jan = 'i', Feb = 'i', Mar = 'i',
                                        Apr = 'i', May = 'i', Jun = 'i'))
dat <- read_table("data.txt",
                  col_types = list(Jul = col_skip(), Aug = col_skip(), Sep = col_skip(),
                                   Oct = col_skip(), Nov = col_skip(), Dec = col_skip()))
dat <- read_table("data.txt", col_types = 'iiiiiii______')
Run Code Online (Sandbox Code Playgroud)

从文档中使用的字符的解释col_types:

每个字符代表一列:c =字符,i =整数,n =数字,d = double,l =逻辑,D =日期,T =日期时间,t =时间,?= guess,或_/ - 跳过列

  • @Deleet您可以使用`fread`来读取这样的大型压缩文件:`fread("gunzip -c data.txt.gz",drop = c(8:13))`. (2认同)

Rah*_*raj 8

您也可以使用JDBC来实现此目的.让我们创建一个示例csv文件.

write.table(x=mtcars, file="mtcars.csv", sep=",", row.names=F, col.names=T) # create example csv file
Run Code Online (Sandbox Code Playgroud)

从以下链接下载并保存CSV JDBC驱动程序:http://sourceforge.net/projects/csvjdbc/files/latest/download

> library(RJDBC)

> path.to.jdbc.driver <- "jdbc//csvjdbc-1.0-18.jar"
> drv <- JDBC("org.relique.jdbc.csv.CsvDriver", path.to.jdbc.driver)
> conn <- dbConnect(drv, sprintf("jdbc:relique:csv:%s", getwd()))

> head(dbGetQuery(conn, "select * from mtcars"), 3)
   mpg cyl disp  hp drat    wt  qsec vs am gear carb
1   21   6  160 110  3.9  2.62 16.46  0  1    4    4
2   21   6  160 110  3.9 2.875 17.02  0  1    4    4
3 22.8   4  108  93 3.85  2.32 18.61  1  1    4    1

> head(dbGetQuery(conn, "select mpg, gear from mtcars"), 3)
   MPG GEAR
1   21    4
2   21    4
3 22.8    4
Run Code Online (Sandbox Code Playgroud)


jar*_*rot 5

vroom提供了一种在导入期间按名称选择/删除列的“整洁”方法。文档:https://www.tidyverse.org/blog/2019/05/vroom-1-0-0/#column-selection

\n

列选择(col_select)

\n

vroom 参数“col_select”使得选择要保留(或省略)的列更加简单。col_select 的接口与 dplyr::select() 相同。

\n按名称选择列\n
data <- vroom("flights.tsv", col_select = c(year, flight, tailnum))\n#> Observations: 336,776\n#> Variables: 3\n#> chr [1]: tailnum\n#> dbl [2]: year, flight\n#> \n#> Call `spec()` for a copy-pastable column specification\n#> Specify the column types with `col_types` to quiet this message\n
Run Code Online (Sandbox Code Playgroud)\n按名称删除列\n
data <- vroom("flights.tsv", col_select = c(-dep_time, -air_time:-time_hour))\n#> Observations: 336,776\n#> Variables: 13\n#> chr [4]: carrier, tailnum, origin, dest\n#> dbl [9]: year, month, day, sched_dep_time, dep_delay, arr_time, sched_arr_time, arr...\n#> \n#> Call `spec()` for a copy-pastable column specification\n#> Specify the column types with `col_types` to quiet this message\nUse the selection helpers\ndata <- vroom("flights.tsv", col_select = ends_with("time"))\n#> Observations: 336,776\n#> Variables: 5\n#> dbl [5]: dep_time, sched_dep_time, arr_time, sched_arr_time, air_time\n#> \n#> Call `spec()` for a copy-pastable column specification\n#> Specify the column types with `col_types` to quiet this message\n
Run Code Online (Sandbox Code Playgroud)\n或按名称重命名列\n
data <- vroom("flights.tsv", col_select = list(plane = tailnum, everything()))\n#> Observations: 336,776\n#> Variables: 19\n#> chr  [ 4]: carrier, tailnum, origin, dest\n#> dbl  [14]: year, month, day, dep_time, sched_dep_time, dep_delay, arr_time, sched_arr...\n#> dttm [ 1]: time_hour\n#> \n#> Call `spec()` for a copy-pastable column specification\n#> Specify the column types with `col_types` to quiet this message\ndata\n#> # A tibble: 336,776 x 19\n#>    plane  year month   day dep_time sched_dep_time dep_delay arr_time\n#>    <chr> <dbl> <dbl> <dbl>    <dbl>          <dbl>     <dbl>    <dbl>\n#>  1 N142\xe2\x80\xa6  2013     1     1      517            515         2      830\n#>  2 N242\xe2\x80\xa6  2013     1     1      533            529         4      850\n#>  3 N619\xe2\x80\xa6  2013     1     1      542            540         2      923\n#>  4 N804\xe2\x80\xa6  2013     1     1      544            545        -1     1004\n#>  5 N668\xe2\x80\xa6  2013     1     1      554            600        -6      812\n#>  6 N394\xe2\x80\xa6  2013     1     1      554            558        -4      740\n#>  7 N516\xe2\x80\xa6  2013     1     1      555            600        -5      913\n#>  8 N829\xe2\x80\xa6  2013     1     1      557            600        -3      709\n#>  9 N593\xe2\x80\xa6  2013     1     1      557            600        -3      838\n#> 10 N3AL\xe2\x80\xa6  2013     1     1      558            600        -2      753\n#> # \xe2\x80\xa6 with 336,766 more rows, and 11 more variables: sched_arr_time <dbl>,\n#> #   arr_delay <dbl>, carrier <chr>, flight <dbl>, origin <chr>,\n#> #   dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,\n#> #   time_hour <dttm>\n
Run Code Online (Sandbox Code Playgroud)\n