Data and Basic Calculations with R

Raesup Kim
4 min readJun 12, 2020

In my last post (Starting Out with R Language), I mentioned that I would cover the deeper contents regarding to statistics and visualization with R in the following posts. So, for today, I am going to talk about the data and basic calculations & functions with R.

Before we go through the calculations and functions, I would like to cover the type of object and data with R.

Types of object

There are some basic object types that appear in routine with R calculations like numeric, character and logical.

Numeric

Numeric data type is used to represent decimal values which is default computational data type in R.

> n<- 1
> mode(n)
[1] “numeric”

Character

Character data type is string values in R.

> c <- “Hello World”
> mode(c)
[1] “character”

Logical

Logical data type is boolean value which is either of the two values true and false. In R, logical data type must be declared with capital letters such as TRUE, FALSE, T or F.

> t <- TRUE
> mode(t)
[1] “logical”

> f <- F
> mode(f)
[1] “logical”

Types of data

R has a lot of data types such as vectors, matrices, and data frames. Let’s talk about what data types there are and how data types are generated.

Vectors

Vector is a fundamental data type in R and it has only one object type in each vector. When the vector data type has only one component in it, we call this data type as scalar. In order to create vector data type, we can use combine function which has syntax like “c(c1, c2, c3, …)” and also can use sequence function which has syntax like “seq(1,10)”.

> v1 <- c(1,2,3) #numeric vector
> v1
[1] 1 2 3

> v2 <- c(“apple”, “grape”, “melon”) #character vector
> v2
[1] “apple” “grape” “melon”

> v3 <- c(TRUE, T, FALSE) #logical vector
> v3
[1] TRUE TRUE FALSE

> v4 <- seq(1,10)
> v4
[1] 1 2 3 4 5 6 7 8 9 10

> v5 <- 1:10
> v5
[1] 1 2 3 4 5 6 7 8 9 10

Matrix

Matrix data type has a collection of data components arranged in a two-dimensional rectangular layout which means it has multiple vectors in it. It consists of rows & columns and all of the vector data types have to be same one. In order to create matrix data type, we can use matrix function which has syntax like “matrix(c(1,2,3, …), nrow=x, ncol=y)”.

Data Frame

Data frame data type is a type that can contain multiple vector data types. It is similar to the table in DBMS. In order to create data frame data type, you can use data frame function which has syntax like “data.frame(vector1, vector2, vector3, …)”. Also, you can use view function to make the data frame into table format.

I believe that we are ready to cover some basic calculations and functions with R now.

Basic Calculations

We are able to do simple calculations and also define simple functions and calculate it.

Simple calculations

We can do various calculations like sum, subtraction, multiplication, and so on with numeric values or numeric objects.

> a <- 25
> a
[1] 25

> b <- 5
> b
[1] 5

> c <- a + b
> c
[1] 30

> d <- a — b
> d
[1] 20

> e <- a * b
> e
[1] 125

> f <- a / b
> f
[1] 5

Basic functions

We can use some basic functions for calculations such as median and mean. Median is for seeking the middle value among the components and mean is for calculating the average of the components.

> z <- c(seq(1,10,2),seq(11,20,3))
> z
[1] 1 3 5 7 9 11 14 17 20
> median(z)
[1] 9
> mean(z)
[1] 9.666667

Also, we can create and define our own functions.

> f1 <- function(x,y){x+y}
> f1(10,5)
[1] 15

> f2 <- function(x,y){x*y}
> f2(10,5)
[1] 50

Now, I am pretty sure that we have gotten a sense of how the data and basic calculations work in R. For the next post, I am going to cover the packages and visualization with R.

--

--