Objects and Functions in R

Author

Abdullah Al Shamim

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.”

Systemic Foundations: Objects, Functions, and Automation

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.

Phase 1: Object Assignment & Mathematical Functions

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.

Code
week <- 7
month <- 30

# Passing objects as arguments into a function
total_days <- sum(week, month)
print(total_days)
[1] 37

A vector is a collection of values. R uses NA to represent missing data, which can “break” functions if not handled.

Code
# Creating a vector with a missing value
odd_numbers <- c(1, 3, 5, 7, NA, 11)

# This returns NA because of the missing value
mean(odd_numbers)
[1] NA
Code
# Systemic Fix: Tell the function to 'Remove NA'
mean(odd_numbers, na.rm = TRUE)
[1] 5.4

Phase 2: Structural Exploration (Audit)

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.

Code
df <- chickwts

head(df)        # First 6 rows
  weight      feed
1    179 horsebean
2    160 horsebean
3    136 horsebean
4    227 horsebean
5    217 horsebean
6    168 horsebean
Code
tail(df)        # Last 6 rows
   weight   feed
66    352 casein
67    359 casein
68    216 casein
69    222 casein
70    283 casein
71    332 casein
Code
class(df)       # Object type (data.frame)
[1] "data.frame"
Code
unique(df$feed) # Distinct categories in a column
[1] horsebean linseed   soybean   sunflower meatmeal  casein   
Levels: casein horsebean linseed meatmeal soybean sunflower

Quickly understanding the distribution of numbers.

Code
# Overall summary
summary(df)
     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  
Code
# Specific column summary
summary(df$weight)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  108.0   204.5   258.0   261.3   323.5   423.0 

Phase 3: Subsetting & Indexing

Subsetting is the act of “slicing” an object to extract only what you need. R uses the [rows, columns] format.

Code
# Extracting a specific slice: Rows 11 to 15, Columns 1 to 2
extracted <- df[11:15, 1:2]
Code
# Accessing a single column using the $ sign
column_data <- df$weight
length(column_data) # Check how many entries exist
[1] 71

Phase 4: Custom Functions for Beginners (Automation)

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.

Code
temp_converter <- function(celsius) {
  fahrenheit <- (celsius * 9/5) + 32
  return(fahrenheit)
}

# Usage
temp_converter(25) # Returns 77
[1] 77

A practical function to find what percentage a part is of a total.

Code
calc_percent <- function(part, total) {
  percentage <- (part / total) * 100
  return(round(percentage, 2)) # Rounding for a clean output
}

# Usage: What is 15 out of 60?
calc_percent(15, 60) # Returns 25
[1] 25

A pro-level beginner function that automatically handles missing data (NA).

Code
safe_mean <- function(my_vector) {
  result <- mean(my_vector, na.rm = TRUE)
  return(result)
}

# Usage: No need to type na.rm every time!
safe_mean(odd_numbers)
[1] 5.4

🎓 Systemic Summary for Learners

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.

Pro-Learner Tip: The “Don’t Attach” Rule

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.