参考 Schouwenaars (2016)
dir("datasets/")
## [1] "hotdogs.txt" "potatoes.csv" "potatoes.txt"
## [4] "swimming_pools.csv" "urbanpop.xls" "urbanpop.xlsx"
properties <- c("area", "temp", "size", "storage", "method",
"texture", "flavor", "moistness")
library(readr)
potatoes_fragment <- read_tsv("datasets/potatoes.txt",skip = 2, n_max = 3, col_names = properties)
## Parsed with column specification:
## cols(
## area = col_double(),
## temp = col_double(),
## size = col_double(),
## storage = col_double(),
## method = col_double(),
## texture = col_double(),
## flavor = col_double(),
## moistness = col_double()
## )
potatoes_fragment
## # A tibble: 3 x 8
## area temp size storage method texture flavor moistness
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 1 1 1 3 2.5 2.8 2.8
## 2 1 1 1 1 4 2.1 2.9 2.4
## 3 1 1 1 1 5 1.9 2.8 2.2
其他的 read_*
也可以加上 n_max
,触类旁通。
You can manually set the types with a string, where each character denotes the class of the column:
c
haracter,d
ouble,i
nteger andl
ogical.
这是 R 的四种数据。
# readr is already loaded
# Column names
properties <- c("area", "temp", "size", "storage", "method",
"texture", "flavor", "moistness")
# Import all data, but force all columns to be character: potatoes_char
potatoes_char <- read_tsv("datasets/potatoes.txt", col_types = "cccccccc", col_names = properties,
n_max = 3
)
# Print out structure of potatoes_char
str(potatoes_char)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 3 obs. of 8 variables:
## $ area : chr "1" "1" "1"
## $ temp : chr "1" "1" "1"
## $ size : chr "1" "1" "1"
## $ storage : chr "1" "1" "1"
## $ method : chr "1" "2" "3"
## $ texture : chr "2.9" "2.3" "2.5"
## $ flavor : chr "3.2" "2.5" "2.8"
## $ moistness: chr "3.0" "2.6" "2.8"
## - attr(*, "spec")=
## .. cols(
## .. area = col_character(),
## .. temp = col_character(),
## .. size = col_character(),
## .. storage = col_character(),
## .. method = col_character(),
## .. texture = col_character(),
## .. flavor = col_character(),
## .. moistness = col_character()
## .. )