선택된 열만 읽습니다.
누구든지 아래 데이터를 매년 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
데이터가 file에 있다고 가정하면 인수를 data.txt
사용하여 열을 건너 뛸 수 있습니다. 여기에서 처음 7 개 열의 데이터가 있고 나머지 6 개 열을 건너 뛰어야 함 을 나타냅니다.colClasses
read.table()
"integer"
"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
실제 데이터 유형 "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
를 사용하여 만들어졌습니다
write.table(dat, file = "data.txt", row.names = FALSE)
곳 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))
열 수를 미리 알 수없는 경우 유틸리티 기능 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"))
데이터 세트에서 특정 열 집합을 읽으려면 몇 가지 다른 옵션이 있습니다.
1)으로 fread
로부터 data.table
-package :
당신은으로 원하는 열을 지정할 수 있습니다 select
에서 매개 변수 fread
로부터 data.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))
또는 drop
매개 변수를 사용하여 읽을 열을 표시 할 수 있습니다 .
dat <- fread("data.txt", drop = c("Jul","Aug","Sep","Oct","Nov","Dec"))
dat <- fread("data.txt", drop = c(8:13))
모든 결과 :
> 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
업데이트 : data.tablefread
을 반환 하지 않으려면 -parameter를 사용하십시오 ( 예 :data.table = FALSE
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")
3) read_*
-패키지 의 -기능으로 readr
:
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______')
From the documentation an explanation for the used characters with col_types
:
each character represents one column: c = character, i = integer, n = number, d = double, l = logical, D = date, T = date time, t = time, ? = guess, or _/- to skip the column
You could also use JDBC to achieve this. Let's create a sample csv file.
write.table(x=mtcars, file="mtcars.csv", sep=",", row.names=F, col.names=T) # create example csv file
Download and save the the CSV JDBC driver from this link: 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
참고URL : https://stackoverflow.com/questions/5788117/only-read-selected-columns
'programing tip' 카테고리의 다른 글
세션 도용을 방지하는 가장 좋은 방법은 무엇입니까? (0) | 2020.07.11 |
---|---|
Android에서 한 활동에서 다른 활동으로 오브젝트를 전달하려면 어떻게해야합니까? (0) | 2020.07.11 |
HTML 양식 : 선택 옵션과 데이터 목록 옵션 (0) | 2020.07.11 |
git을 Xcode와 통합 할 수 있습니까? (0) | 2020.07.11 |
iOS / macOS에서 프로그래밍 방식으로 내 IP 주소를 얻는 방법은 무엇입니까? (0) | 2020.07.11 |