<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>R on timerring</title><link>https://blogs.timerring.com/tags/r/</link><description>Recent content in R on timerring</description><generator>Hugo</generator><language>en</language><lastBuildDate>Sun, 25 Feb 2024 19:47:10 +0800</lastBuildDate><atom:link href="https://blogs.timerring.com/tags/r/index.xml" rel="self" type="application/rss+xml"/><item><title>Data Export</title><link>https://blogs.timerring.com/posts/04-data-export/</link><pubDate>Sun, 25 Feb 2024 19:47:10 +0800</pubDate><guid>https://blogs.timerring.com/posts/04-data-export/</guid><description>&lt;h2 id="1-export-data">
 1. Export data
 &lt;a class="anchor" href="#1-export-data">#&lt;/a>
&lt;/h2>
&lt;p>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 &lt;code>write.table( )&lt;/code> and &lt;code>write.csv( )&lt;/code> can export data to a .txt file and a .csv file, respectively.&lt;/p>
&lt;p>In addition, the function &lt;code>save( )&lt;/code> can save the specified object in the workspace as an R data file with the extension &lt;code>.rdata&lt;/code>. For example:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-R" data-lang="R">&lt;span style="display:flex;">&lt;span>ID &lt;span style="color:#666">&amp;lt;-&lt;/span> &lt;span style="color:#666">1&lt;/span>&lt;span style="color:#666">:&lt;/span>&lt;span style="color:#666">5&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>sex &lt;span style="color:#666">&amp;lt;-&lt;/span> &lt;span style="color:#00f">c&lt;/span>(&lt;span style="color:#ba2121">&amp;#34;male&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;female&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;male&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;female&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;male&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>age &lt;span style="color:#666">&amp;lt;-&lt;/span> &lt;span style="color:#00f">c&lt;/span>(&lt;span style="color:#666">25&lt;/span>, &lt;span style="color:#666">34&lt;/span>, &lt;span style="color:#666">38&lt;/span>, &lt;span style="color:#666">28&lt;/span>, &lt;span style="color:#666">52&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>pain &lt;span style="color:#666">&amp;lt;-&lt;/span> &lt;span style="color:#00f">c&lt;/span>(&lt;span style="color:#666">1&lt;/span>, &lt;span style="color:#666">3&lt;/span>, &lt;span style="color:#666">2&lt;/span>, &lt;span style="color:#666">2&lt;/span>, &lt;span style="color:#666">3&lt;/span>) 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>pain.f &lt;span style="color:#666">&amp;lt;-&lt;/span> &lt;span style="color:#00f">factor&lt;/span>(pain, levels &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#666">1&lt;/span>&lt;span style="color:#666">:&lt;/span>&lt;span style="color:#666">3&lt;/span>, labels &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#00f">c&lt;/span>(&lt;span style="color:#ba2121">&amp;#34;mild&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;medium&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;severe&amp;#34;&lt;/span>)) 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>patients &lt;span style="color:#666">&amp;lt;-&lt;/span> &lt;span style="color:#00f">data.frame&lt;/span>(ID, sex, age, pain.f)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">save&lt;/span>(patients, file &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#ba2121">&amp;#34;patients.rdata&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#408080;font-style:italic"># IMPORT DATA&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">load&lt;/span>(&lt;span style="color:#ba2121">&amp;#34;patients.rdata&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>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 &lt;code>load( )&lt;/code> function.&lt;/p></description></item><item><title>Strategies for Processing Large Datasets</title><link>https://blogs.timerring.com/posts/09-strategies-for-large-datasets/</link><pubDate>Sun, 25 Feb 2024 19:47:08 +0800</pubDate><guid>https://blogs.timerring.com/posts/09-strategies-for-large-datasets/</guid><description>&lt;p>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 &lt;strong>use a 64-bit operating system and a device with relatively large memory whenever possible&lt;/strong>. Otherwise, data analysis may take too long or may even be impossible to carry out. In addition, &lt;strong>effective strategies for processing data&lt;/strong> can greatly improve analysis efficiency.&lt;/p></description></item><item><title>Numerical Descriptive Analysis</title><link>https://blogs.timerring.com/posts/12-numerical-descriptive-analysis/</link><pubDate>Sun, 20 Aug 2023 22:56:22 +0800</pubDate><guid>https://blogs.timerring.com/posts/12-numerical-descriptive-analysis/</guid><description>&lt;p>Before the analysis, first convert the categorical variables low, race, smoke, ht, and ui in the birthwt dataset into factors.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-R" data-lang="R">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">library&lt;/span>(MASS)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">data&lt;/span>(birthwt)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">str&lt;/span>(birthwt)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">options&lt;/span>(warn&lt;span style="color:#666">=&lt;/span>&lt;span style="color:#666">-1&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">library&lt;/span>(dplyr)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>birthwt &lt;span style="color:#666">&amp;lt;-&lt;/span> birthwt &lt;span style="color:#666">%&amp;gt;%&lt;/span> 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#00f">mutate&lt;/span>(low &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#00f">factor&lt;/span>(low, labels &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#00f">c&lt;/span>(&lt;span style="color:#ba2121">&amp;#34;no&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;yes&amp;#34;&lt;/span>)),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> race &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#00f">factor&lt;/span>(race, labels &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#00f">c&lt;/span>(&lt;span style="color:#ba2121">&amp;#34;white&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;black&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;other&amp;#34;&lt;/span>)),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> smoke &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#00f">factor&lt;/span>(smoke, labels &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#00f">c&lt;/span>(&lt;span style="color:#ba2121">&amp;#34;no&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;yes&amp;#34;&lt;/span>)),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> ht &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#00f">factor&lt;/span>(ht, labels &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#00f">c&lt;/span>(&lt;span style="color:#ba2121">&amp;#34;no&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;yes&amp;#34;&lt;/span>)),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> ui &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#00f">factor&lt;/span>(ui, labels &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#00f">c&lt;/span>(&lt;span style="color:#ba2121">&amp;#34;no&amp;#34;&lt;/span>, &lt;span style="color:#ba2121">&amp;#34;yes&amp;#34;&lt;/span>)))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">str&lt;/span>(birthwt)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>ggplot2 and Other Plots</title><link>https://blogs.timerring.com/posts/11-ggplot2-and-other-plots/</link><pubDate>Sat, 19 Aug 2023 22:19:09 +0800</pubDate><guid>https://blogs.timerring.com/posts/11-ggplot2-and-other-plots/</guid><description>&lt;h2 id="1-getting-to-know-the-ggplot2-package">
 1. Getting to Know the ggplot2 Package
 &lt;a class="anchor" href="#1-getting-to-know-the-ggplot2-package">#&lt;/a>
&lt;/h2>
&lt;p>The ggplot2 package provides a plotting system based on a grammar of layers. It remedies the lack of consistency among functions in R&amp;rsquo;s base graphics system and raises R&amp;rsquo;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).&lt;/p></description></item><item><title>Plotting with R's Base Graphics System</title><link>https://blogs.timerring.com/posts/10-plotting-with-base-r/</link><pubDate>Sat, 19 Aug 2023 21:33:26 +0800</pubDate><guid>https://blogs.timerring.com/posts/10-plotting-with-base-r/</guid><description>&lt;p>R&amp;rsquo;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.&lt;/p>
&lt;p>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( ).&lt;/p></description></item><item><title>Handling Missing Values</title><link>https://blogs.timerring.com/posts/08-handling-missing-values/</link><pubDate>Sat, 19 Aug 2023 20:03:10 +0800</pubDate><guid>https://blogs.timerring.com/posts/08-handling-missing-values/</guid><description>&lt;h2 id="handling-missing-values">
 Handling Missing Values
 &lt;a class="anchor" href="#handling-missing-values">#&lt;/a>
&lt;/h2>
&lt;p>In actual data analysis, missing data are frequently encountered. Missing values usually occur because data were not collected or were not entered.&lt;/p>
&lt;p>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.&lt;/p></description></item><item><title>Merging Data Frames</title><link>https://blogs.timerring.com/posts/07-merging-data-frames/</link><pubDate>Sat, 19 Aug 2023 14:19:21 +0800</pubDate><guid>https://blogs.timerring.com/posts/07-merging-data-frames/</guid><description>&lt;p>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.&lt;/p></description></item><item><title>Using the dplyr Package</title><link>https://blogs.timerring.com/posts/06-using-dplyr/</link><pubDate>Fri, 18 Aug 2023 21:35:27 +0800</pubDate><guid>https://blogs.timerring.com/posts/06-using-dplyr/</guid><description>&lt;p>This package handles data frames more efficiently with a unified specification. ** The first parameter of all functions that process data frames in the &lt;code>dplyr&lt;/code> package is the data frame name. **&lt;/p>
&lt;p>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.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-R" data-lang="R">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">library&lt;/span>(dplyr)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">data&lt;/span>(birthwt, package &lt;span style="color:#666">=&lt;/span> &lt;span style="color:#ba2121">&amp;#34;MASS&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#408080;font-style:italic"># ??birthwt&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>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&amp;rsquo;s age, race, smoking status, and history of hypertension (ht).&lt;/p></description></item><item><title>Using Base R</title><link>https://blogs.timerring.com/posts/05-using-base-r/</link><pubDate>Thu, 17 Aug 2023 23:05:23 +0800</pubDate><guid>https://blogs.timerring.com/posts/05-using-base-r/</guid><description>&lt;h2 id="using-base-r">
 Using Base R
 &lt;a class="anchor" href="#using-base-r">#&lt;/a>
&lt;/h2>
&lt;p>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.&lt;/p>
&lt;p>Load a small dataset &lt;code>Familydata&lt;/code> in the &lt;code>epiDisplay&lt;/code> package first.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style=";-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-R" data-lang="R">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">library&lt;/span>(epiDisplay)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#00f">data&lt;/span>(&lt;span style="color:#ba2121">&amp;#34;Familydata&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Data Acquisition</title><link>https://blogs.timerring.com/posts/03-data-acquisition/</link><pubDate>Thu, 17 Aug 2023 21:27:32 +0800</pubDate><guid>https://blogs.timerring.com/posts/03-data-acquisition/</guid><description>&lt;p>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.&lt;/p>
&lt;p>R provides data import tools with a wide range of applications.&lt;/p></description></item><item><title>Data Structures and Conversion</title><link>https://blogs.timerring.com/posts/02-data-structures-and-conversion/</link><pubDate>Wed, 16 Aug 2023 20:08:02 +0800</pubDate><guid>https://blogs.timerring.com/posts/02-data-structures-and-conversion/</guid><description>&lt;p>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.&lt;/p></description></item><item><title>Introduction to R</title><link>https://blogs.timerring.com/posts/01-introduction-to-r/</link><pubDate>Tue, 15 Aug 2023 22:19:41 +0800</pubDate><guid>https://blogs.timerring.com/posts/01-introduction-to-r/</guid><description>&lt;h2 id="common-arithmetic-operators">
 Common Arithmetic Operators
 &lt;a class="anchor" href="#common-arithmetic-operators">#&lt;/a>
&lt;/h2>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th style="text-align: left">Operator&lt;/th>
 &lt;th style="text-align: left">Description&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td style="text-align: left">+&lt;/td>
 &lt;td style="text-align: left">Addition&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">−&lt;/td>
 &lt;td style="text-align: left">Subtraction&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">*&lt;/td>
 &lt;td style="text-align: left">Multiplication&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">/&lt;/td>
 &lt;td style="text-align: left">Division&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">^ or **&lt;/td>
 &lt;td style="text-align: left">Exponentiation&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">%%&lt;/td>
 &lt;td style="text-align: left">Remainder&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">%/%&lt;/td>
 &lt;td style="text-align: left">Integer division&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;p>Note that R is case-sensitive.&lt;/p></description></item></channel></rss>