Basic command and data types

Author

Hassan Ghayas

Published

April 26, 2026

Variable

Variables are objects in R use to store values. It can be single value, basic or complex arithmetic operations, or a data matrix or a data frame. Variable can be assign using either the “<-” or “=” operators. (alt+ -)

a = 5
b <- 8*3
c = "Data analysis with R"
a + b
[1] 29
c
[1] "Data analysis with R"
cat(a,c)
5 Data analysis with R

Basic command

x <-  3
y <-  2
x+y
[1] 5
class(x)
[1] "numeric"
class(y)
[1] "numeric"
x <- as.integer(x)
class(x)
[1] "integer"
sqrt(3 * 4 + 2 * 5 + 3)
[1] 5
log(5) + log(10)
[1] 3.912023
log(50) 
[1] 3.912023
sum(3 * 4 + 2 * 5 + 3)
[1] 25

Data Type

Variables can contain values of specific types within R. The six data types that R uses include:

  • "numeric" —> any numerical value, including whole numbers and decimals.

  • "character" —> text values, denoted by using quotes (““) around text.

  • "integer" —> whole numbers (e.g., 2L, the L indicates to R that it’s an integer). It behaves similar to the numeric data type for most tasks or functions; however, it takes up less storage space than numeric data.

  • "logical" —> TRUE and FALSE (the Boolean data type). The logical data type can be specified using four values, TRUE in all capital letters, FALSE in all capital letters, a single capital T or a single capital F.

Data Structure

Vector

A vector is substantially a list of variables. A vector consists of a collection of numbers, arithmetic expressions, logical values or character strings for example. However, each vector must have all components of the same type, i.e numeric, character, or logical

c(3,8,2)
[1] 3 8 2
c("mike", "John")
[1] "mike" "John"
c(5,9,"abc", TRUE)
[1] "5"    "9"    "abc"  "TRUE"
numbers <- c(38,26,35)
name <- c("mike", "john", "jane")
any_text <- c(5,9,"abc", TRUE)
class(numbers)
[1] "numeric"
class(name)
[1] "character"
class(any_text)
[1] "character"

Data Frame

Two-dimensional, tabular data structure used to store data where each column represents a variable and each row represents an observation

df <- data.frame(name, numbers)
head(df)
  name numbers
1 mike      38
2 john      26
3 jane      35
colnames(df)[2] <- "age"
names(df)[names(df)=="numbers"] <- "age"
summary(df$age)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   26.0    30.5    35.0    33.0    36.5    38.0 

Matrix

A matrix in R is a collection of vectors of same length and identical datatype

data_matrix <- as.matrix(cbind(a=numbers,b=2*numbers,c=numbers/1.5))
data_matrix
      a  b        c
[1,] 38 76 25.33333
[2,] 26 52 17.33333
[3,] 35 70 23.33333