timerring

Merging Data Frames

August 19, 2023 · 4 min read
Tutorial
R

Sometimes datasets come from multiple places, and we need to merge two or more datasets into one dataset. Operations for merging data frames include vertical merging, horizontal merging, and merging by a shared variable.

...


Using the dplyr Package

August 18, 2023 · 9 min read
Tutorial
R

This package handles data frames more efficiently with a unified specification. ** The first parameter of all functions that process data frames in the dplyr package is the data frame name. **

Taking the birthwt dataset in the MASS package as an example, the following describes the use of commonly used functions in the dplyr package. This data set comes from a case-control study of risk factors for low birth weight in newborns. Start by loading the dataset and viewing its related information.

library(dplyr)
data(birthwt, package = "MASS")
# ??birthwt

A total of 189 subjects and 10 variables were included in the dataset birthwt. where the outcome variable bwt is the weight of the newborn (unit: g) and the variable low is a binary classification variable that converts the value of bwt into 2500g points. The remaining 8 variables are predictors, including pregnant women’s age, race, smoking status, and history of hypertension (ht).

...


Using Base R

August 17, 2023 · 10 min read
Tutorial
R

Using Base R #

In actual data analysis, analysts often spend a lot of effort on data preparation, transforming data into the form required for analysis. Unfortunately, most statistics textbooks rarely address this important issue. Organizing data is one of the tasks of statistics. We started to focus on the most common data format in R - the basic operation of data frames. We will first process the data frames using the base package.

Load a small dataset Familydata in the epiDisplay package first.

library(epiDisplay)
data("Familydata")
...


Data Acquisition

August 17, 2023 · 5 min read
Tutorial
R

In fact, R has a large number of built-in datasets available for analysis and practice, and we can also create data in R that simulate specific distributions. In actual work, however, data analysts more often face external data from various data sources, namely data files with all kinds of extensions, such as .txt, .csv, .xlsx, .xls, and so on. Files with different extensions represent different file formats, which often causes trouble for analysts.

R provides data import tools with a wide range of applications.

...


Organizing Code with Design Patterns

August 17, 2023 · 20 min read
Tutorial
Python

好的设计模式方便对代码进行功能扩展/修改,降低耦合性,提高复用性。

from abc import abstractmethod, ABC
from typing import Callable, final, Tuple, List, Sequence, Dict
from random import choices, randrange
from functools import wraps
from numpy import ndarray
from pandas import DataFrame
from time import perf_counter, sleep
from datetime import datetime, timedelta
...


Data Structures and Conversion

August 16, 2023 · 11 min read
Tutorial
R

The first step in any data analysis is to create a dataset in the required format. In R, this task consists of two steps: first select a data structure to store the data, and then enter or import the data into this data structure. The following introduces the various data structures used to store data in R.

...


Dividing Functionality with Classes

August 16, 2023 · 31 min read
Tutorial
Python

理解类是运用设计模式的前提;运用类本身也能更好的组织代码,提升扩展性、可读性、复用性。

from typing import Callable, Tuple, List, Sequence
from pandas import DataFrame
...


Writing Maintainable Code

August 16, 2023 · 15 min read
Tutorial
Python

from typing import Callable, Tuple, List
from pandas import DataFrame

项目结构定好了,就开始着手写代码了;那编写代码有哪些注意点呢?

...


Introduction to R

August 15, 2023 · 2 min read
Tutorial
R

Common Arithmetic Operators #

OperatorDescription
+Addition
−Subtraction
*Multiplication
/Division
^ or **Exponentiation
%%Remainder
%/%Integer division

Note that R is case-sensitive.

...


Building a Project Structure

August 15, 2023 · 9 min read
Tutorial
Python

在具体的数据科学场景下,按照项目结构→代码编写→功能划分→组织代码的顺序,组织高质量的代码。

项目结构也可认为是目录结构,例如

Image Name Image Name

我们将项目代码拆分成多个功能模块,固化成函数/类并写到不同的文件中,存放在不同的路径里,python 将每个 py 文件视作为一个模块,可以用 import 调用;例如在 main.py 里调用 /db/db.py 里的函数 create_conn

from db.db import create_conn    
conn = create_conn() # create_conn创建了一个数据库的链接对象    
...