Repo này ghi chép lại cách mà một Data Engineer / Data Science xử lý dữ liệu với R. Mình tự học, đang làm việc về ngành khoa học dữ liệu. Sau Python
thì mình bắt đầu với R, là một ngôn ngữ khá mạnh để xử lý dữ liệu.
Mỗi chương / phần lớn sẽ được đặt trong 1 thư mục cùng với Notebook cụ thể, cập nhật thường xuyên theo những gì mà mình học được. Một số bài viết hoàn chỉnh mà mình đúc kết được cũng có thể được đăng ở https://blog.duyet.net
1. Import data
utils
package (mặc định)
read.csv()
- Đọc data.frame từ csvstringsAsFactors = TRUE
read.delim() & read.table()
- Đọc data.frame từ bất kỳ dạng file tabular nào.summary()
- Thống kê miêu tả nhanh về data.framefile.path()
- Tạo đường dẫn đến data.- Lọc dữ liệu với
which.min
vàwhich.max
- Sử dụng
colClasses
để ignore một số cột khi import data.
Sử dụng package readr
, thư viện này có một số hàm hay để đọc và xử lý data.frame.
read_csv()
read_tsv()
đọc *.tsv file với tham sốcol_names
read_delim()
skip
vàn_max
col_types = "iiiiiddd"
parse datatype.
Xử lý Excel file với readxl
package.
excel_sheets()
liệt kê danh sách sheets name.read_excel()
import data.frame từ excel.
Tham khảo thêm: https://github.com/hadley/readxl
Sử dụng R để kết nối tới CSDL, DBI
package
dbConnect()
kết nối đến CSDLRMySQL::MySQL()
dbListTables()
liệt kê danh sách bảng.- ...
Tham khảo thêm: https://cran.r-project.org/web/packages/DBI/index.html
Thư viện read.csv
, read.tsv
, ... của utils
hoặc readr
có thể đọc trực tiếp file từ URL.
url_csv <- "http://s3.amazonaws.com/assets.datacamp.com/production/course_1478/datasets/swimming_pools.csv"
swimming_pools <- read.csv(url_csv)
Parse JSON với jsonlite
package
fromJSON()
parse từ raw JSON và URL.toJSON()
convert data.frame hoặc list sang JSON string.prettify()
- format JSON dạng "đẹp đẹp" dễ đọc :D -minify()
ngược lại.
The data cleaning process:
- Preparing data for analysis
- Exploring raw data
- Tidying data
head()
vàtail
để xem 1 phầ n của dataframe.str(data.frame)
xem cấu trúc dataframe.dim()
chiều dữ liệu vànames(df)
xem các cột của df.
Ngoài ra 1 thư viện rất hay được sử dụng là dplyr
glimpse()
có chức năng tương tựstr()
summary(Mumbai2013)
summary data.
Visualizing data với 1 số hàm cơ bản
- Fork the project on Github
- Create a topic branch for your changes
- Ensure that you provide documentation and test coverage for your changes (patches won’t be accepted without)
- Create a pull request on Github (these are also a great place to start a conversation around a patch as early as possible)
MIT License
Copyright (c) 2016 Van-Duyet Le
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.