Functions and Objects in R for Beginners

Author

Abdullah Al Shamim

Objects and Basic Functions

Note

In R, an Object is a container (like a variable) that holds data. A Function is a command (followed by parentheses ()) that performs an action on that data.

1. Assignments & Math

This is how we store information in R using the assignment operator <-.

Code
# Assigning values to objects
week <- 7
month <- 30

# Using a function to add objects together
sum(week, month)
[1] 37

2. Data Exploration

Once you have a dataset (like chickwts), you need to inspect it.

Code
df <- chickwts

# Look at the first 6 rows and last 6 rows
head(df)
  weight      feed
1    179 horsebean
2    160 horsebean
3    136 horsebean
4    227 horsebean
5    217 horsebean
6    168 horsebean
Code
tail(df)
   weight   feed
66    352 casein
67    359 casein
68    216 casein
69    222 casein
70    283 casein
71    332 casein
Code
# See the data types (Dataframe, Factor, Numeric)
class(df)
[1] "data.frame"
Code
class(df$feed)
[1] "factor"
Code
# Get a full statistical overview
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  

3. Working with Columns

You can work with specific parts of a dataset or get unique information from columns.

Code
# unique() shows categories without repeats
unique(df$feed)
[1] horsebean linseed   soybean   sunflower meatmeal  casein   
Levels: casein horsebean linseed meatmeal soybean sunflower
Code
# attach() lets you refer to columns directly by name
attach(df)
class(weight)
[1] "numeric"
Code
length(weight) # Number of observations
[1] 71

4. Subsetting

If you don’t need the whole dataset, you can “slice” it using square brackets [rows, columns].

Code
# Extracting rows 11 to 15 and columns 1 to 2
extracted <- df[11:15, 1:2]
extracted
   weight    feed
11    309 linseed
12    229 linseed
13    181 linseed
14    141 linseed
15    260 linseed

5. Handling Missing Data (NA)

R is very precise. If your data has a missing value (NA), most functions will fail unless you tell them how to handle it.

Code
odd_numbers <- c(1, 3, 5, 7, NA, 11)

# This results in NA because R doesn't know how to calculate 'NA'
mean(odd_numbers)
[1] NA
Code
# na.rm = TRUE tells R to "Remove NAs" before calculating
mean(odd_numbers, na.rm = TRUE)
[1] 5.4

6. Basic Visualization

R has built-in functions for quick data inspection through plots.

Code
# A quick scatter plot matrix
plot(df)

Code
# A histogram to see the distribution of weight
hist(df$weight, col = "steelblue", main = "Histogram of Weights")

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.