February 25, 2024
·
3 min read
1. Export data
#
Since R is mainly used for data analysis, import files are more commonly used than export files, but sometimes we also need to export data or analysis results. The functions write.table( ) and write.csv( ) can export data to a .txt file and a .csv file, respectively.
In addition, the function save( ) can save the specified object in the workspace as an R data file with the extension .rdata. For example:
ID <- 1:5
sex <- c("male", "female", "male", "female", "male")
age <- c(25, 34, 38, 28, 52)
pain <- c(1, 3, 2, 2, 3)
pain.f <- factor(pain, levels = 1:3, labels = c("mild", "medium", "severe"))
patients <- data.frame(ID, sex, age, pain.f)
save(patients, file = "patients.rdata")
# IMPORT DATA
load("patients.rdata")
the rdata format file takes up less space and loads quickly with R. Therefore, it is recommended that users ** save data in rdata format ** after importing and organizing data in other formats. To import data in this format, simply call the load( ) function.
...
February 25, 2024
·
5 min read
In real-world problems, data analysts may face datasets with hundreds of thousands of records and hundreds of variables. Processing such large datasets requires a relatively large amount of computer memory, so use a 64-bit operating system and a device with relatively large memory whenever possible. Otherwise, data analysis may take too long or may even be impossible to carry out. In addition, effective strategies for processing data can greatly improve analysis efficiency.
...
August 20, 2023
·
4 min read
Before the analysis, first convert the categorical variables low, race, smoke, ht, and ui in the birthwt dataset into factors.
library(MASS)
data(birthwt)
str(birthwt)
options(warn=-1)
library(dplyr)
birthwt <- birthwt %>%
mutate(low = factor(low, labels = c("no", "yes")),
race = factor(race, labels = c("white", "black", "other")),
smoke = factor(smoke, labels = c("no", "yes")),
ht = factor(ht, labels = c("no", "yes")),
ui = factor(ui, labels = c("no", "yes")))
str(birthwt)
...
August 19, 2023
·
13 min read
1. Getting to Know the ggplot2 Package
#
The ggplot2 package provides a plotting system based on a grammar of layers. It remedies the lack of consistency among functions in R’s base graphics system and raises R’s plotting capabilities to an entirely new level. The basic principles of the various data visualizations in ggplot2 are completely consistent: it maps mathematical space to the space of graphical elements. Imagine a blank canvas. On the canvas, we need to define the data to be visualized (data) and the mapping from data variables to graphical properties (mapping).
...
August 19, 2023
·
12 min read
R’s base graphics system was written by Ross Ihaka and is very powerful. It consists mainly of the graphics and grDevices packages, which are loaded automatically when R starts. There are two types of functions in the base graphics system: high-level plotting functions and low-level plotting functions.
High-level plotting functions are functions used to produce plots directly, including plot( ), hist( ), boxplot( ), and pairs( ). Low-level plotting functions are functions used to add new graphics or elements to a plot drawn by a high-level plotting function, including points( ), lines( ), text( ), title( ), legend( ), and axis( ).
...
August 19, 2023
·
8 min read
Handling Missing Values
#
In actual data analysis, missing data are frequently encountered. Missing values usually occur because data were not collected or were not entered.
For example, a missing age may be due to someone not providing his (her) age. Most statistical analysis methods assume that complete datasets are being processed. Therefore, apart from some specialized books, most statistics textbooks rarely address this issue. In fact, before conducting a formal analysis, we need to check whether the dataset contains missing values during the data preparation stage and use some methods to compensate for the loss caused by missing values.
...
August 19, 2023
·
4 min read
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.
...
August 18, 2023
·
9 min read
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).
...
August 17, 2023
·
10 min read
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")
...
August 17, 2023
·
5 min read
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.
...