In [7]:
# Cài đặt `readr` package
devtools::install_github("tidyverse/readr")
In [11]:
# Load the readr package
library(readr)
In [13]:
# Import potatoes.csv with read_csv(): potatoes
potatoes <- read_csv('potatoes.csv')
In [14]:
head(potatoes)
Đọc file tsv, các cột phân cách nhau bằng dấu tab \t
Đọc file potatoes.txt có nội dung như sau:
1 1 1 1 1 2.9 3.2 3.0
1 1 1 1 2 2.3 2.5 2.6
1 1 1 1 3 2.5 2.8 2.8
1 1 1 1 4 2.1 2.9 2.4
1 1 1 1 5 1.9 2.8 2.2
1 1 1 2 1 1.8 3.0 1.7
1 1 1 2 2 2.6 3.1 2.4
...
In [15]:
# Column names
properties <- c("area", "temp", "size", "storage", "method",
"texture", "flavor", "moistness")
# read_tsv
potatoes <- read_tsv('potatoes.txt', col_names=properties)
# head of dataframe
head(potatoes)
In [16]:
properties <- c("area", "temp", "size", "storage", "method",
"texture", "flavor", "moistness")
potatoes <- read_delim('potatoes.txt', col_names=properties, delim='\t')
# head of dataframe
head(potatoes)
In [17]:
# Lấy 10 dòng, kể từ dòng thứ 5
potatoes_fragment <- read_tsv("potatoes.txt", skip = 5, n_max = 10, col_names = properties)
potatoes_fragment
In [18]:
# 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("potatoes.txt", col_types = "iiiiiddd", col_names = properties)
In [20]:
str(potatoes_char)
In [ ]: