Code
[1] 37
To master R, you must embrace its core philosophy: Everything that exists is an Object, and everything that happens is a Function. This systemic approach helps you organize data in your mind as “containers” and the actions you perform on them as “tools.”
This guide moves from basic data storage to advanced custom tools. We will use the built-in chickwts dataset and custom scenarios to build a full conceptual map.
In R, an Object is a saved value in your computer’s memory. A Function is a command that processes these objects to produce a result.
Saving single values (Scalars) to use later.
A vector is a collection of values. R uses NA to represent missing data, which can “break” functions if not handled.
[1] NA
Before analyzing a dataset, you must perform an “Audit” to see its dimensions and data types.
Checking the top, bottom, and “bones” of the data.
weight feed
1 179 horsebean
2 160 horsebean
3 136 horsebean
4 227 horsebean
5 217 horsebean
6 168 horsebean
weight feed
66 352 casein
67 359 casein
68 216 casein
69 222 casein
70 283 casein
71 332 casein
Quickly understanding the distribution of numbers.
weight feed
Min. :108.0 casein :12
1st Qu.:204.5 horsebean:10
Median :258.0 linseed :12
Mean :261.3 meatmeal :11
3rd Qu.:323.5 soybean :14
Max. :423.0 sunflower:12
Subsetting is the act of “slicing” an object to extract only what you need. R uses the [rows, columns] format.
Writing your own functions allows you to bundle complex steps into a single, reusable command. This is where you move from a beginner to a pro.
A function to convert Celsius to Fahrenheit.
A practical function to find what percentage a part is of a total.
| Tool | Action | Systemic Purpose |
<- |
Assignment | Storing data in memory. |
c() |
Concatenate | Building data sequences (vectors). |
$ |
Extraction | Targeting a specific variable in a table. |
na.rm = T |
Cleaning | Preventing missing data from breaking math. |
function() |
Creation | Building your own reusable tools. |
? |
Help | Accessing the manual for any function. |
While attach(df) allows you to call columns directly (like weight), it is not systemic. It creates “masked” objects that lead to errors in complex projects. Always use df$weight or the Tidyverse style to keep your code clear and professional.
Courses that contain short and easy to digest video content are available at premieranalytics.com.bd Each lessons uses data that is built into R or comes with installed packages so you can replicated the work at home. premieranalytics.com.bd also includes teaching on statistics and research methods.