<jupyter_start><jupyter_text>Data Types         Generally, while doing programming in any programming language, you need to use `various variables` to store `various information`. Variables are nothing but reserved memory locations to store values. **This means that, when you create a variable you reserve some space in memory**.You may like to store information of various data types like **character**, **wide character**, **integer**, **floating point**, **double floating point**, **Boolean* etc. Based on the data type of a variable, the operating system `allocates memory` and decides what can be stored in the reserved memory.In contrast to other programming languages like C and java in R, the variables are not declared as some data type. The variables are assigned with R-Objects and the data type of the R-object becomes the data type of the variable. There are many types of `R-objects`. The frequently used ones are:- Vectors- Lists- Matrices- Arrays- Factors- Data FramesThe **simplest** of these objects is the **vector object** and there are `six data types` of these atomic vectors, also termed as six classes of vectors. The other R-Objects are built upon the atomic vectors.                     Six Data Types:                         Logical     `Example:` TRUE, FALSE<jupyter_code># Verify:
x <- TRUE
print(class(x))<jupyter_output>[1] "logical"<jupyter_text>---                    Numeric     `Example:` 5, 12.2, 999<jupyter_code># Verify:
x <- 1.5
print(class(x))<jupyter_output>[1] "numeric"<jupyter_text>---                    Integer     `Example:` 2L, 0L, 101L<jupyter_code># Verify:
x <- 2L
print(class(x))<jupyter_output>[1] "integer"<jupyter_text>---                    Complex     `Example:` 2 + 3i<jupyter_code># Verify:
x <- 2 + 5i
print(class(x))<jupyter_output>[1] "complex"<jupyter_text>---                    Character     `Example:` 'a', "good", "TRUE", '23.5'<jupyter_code># Verify:
x <- "1"
print(class(x))<jupyter_output>[1] "character"<jupyter_text>---                    Raw     `Example:` "Hello" is stored as 48 65 6c 6c 6f<jupyter_code># Verify:
x <- charToRaw("Hello")
print(x)
print(class(x))<jupyter_output>[1] 48 65 6c 6c 6f
[1] "raw"<jupyter_text>In R programming, the very basic data types are the `R-objects` called `vectors` which **hold elements of different classes** as shown above. Please note in R the number of classes is not confined to only the above six types. For example, we can use many atomic vectors and create an array whose class will become array.                 Vectors     When you want to create vector with `more than one element`, you should use `c() function` which means to **combine the elements into a vector**.<jupyter_code># Create a vector:
info <- c(1, 2, 3L, FALSE)

print(info)

# Get the class of the vector:
print(class(info))<jupyter_output>[1] 1 2 3 0
[1] "numeric"<jupyter_text>Lists     A **list** is an `R-object` which can contain many different types of elements inside it like **vectors**, **functions** and even **another list** inside it.<jupyter_code># Create a list:
list1 <- list(info, c(12, 15, 16, 18, 20, 0), FALSE, c("pooya", "maryam"))

# Print the list:
print(list1)<jupyter_output>[[1]]
[1] 1 2 3 0

[[2]]
[1] 12 15 16 18 20  0

[[3]]
[1] FALSE

[[4]]
[1] "pooya"  "maryam"<jupyter_text>Matrices     A matrix is a `two-dimensional` rectangular data set. It can be created using a vector input to the matrix function.<jupyter_code># Create a matrix:
v = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(v)
print(class(v))
print("*******************************************")
M = matrix(data = v, nrow = 2, ncol = 5, byrow = TRUE)
print(M)
print(class(M))
# Create a matrix:
M = matrix(data = c('a', 'a', 'b', 'c', 'b', 1), nrow = 2, ncol = 3, byrow = TRUE)
print(M)<jupyter_output>[,1] [,2] [,3]
[1,] "a"  "a"  "b" 
[2,] "c"  "b"  "1"<jupyter_text>Arrays     While matrices are confined to two dimensions, arrays can be of `any number of dimensions`. The array function takes a dim attribute which creates the required number of dimension. In the below example we create an array with two elements which are 3x3 matrices each.<jupyter_code># Create an array:
a <- array(data = 1:18, dim = c(2,3,3))
print(a)<jupyter_output>, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

, , 3

     [,1] [,2] [,3]
[1,]   13   15   17
[2,]   14   16   18<jupyter_text>Factors     Factors are the r-objects which are created using a vector. It stores the vector along with the distinct values of the elements in the vector as labels. The labels are always character irrespective of whether it is numeric or character or Boolean etc. in the input vector. They are useful in statistical modeling.Factors are created using the factor() function. The nlevels functions gives the count of levels.<jupyter_code># Create a vector:
apple_colors <- c('green', 'green', 'yellow', 'red', 'red', 'red', 'green')
print(class(apple_colors))

# Create a factor object:
factor_apple <- factor(apple_colors)
print(class(factor_apple))

# Print the factor.
print(factor_apple)
print(nlevels(factor_apple))<jupyter_output>[1] "character"
[1] "factor"
[1] green  green  yellow red    red    red    green 
Levels: green red yellow
[1] 3<jupyter_text>Data Frames     `Data frames` are tabular data objects. Unlike a matrix in data frame `each column` can contain `different modes of data`. The first column can be numeric while the second column can be character and third column can be logical. It is a list of vectors of equal length.Data Frames are created using the `data.frame()` function.<jupyter_code>x <- data.frame(name = c("Pooya", "Maryam", "Pedram"),
               age = c(36, 35, 32),
               stu = c(FALSE, TRUE, FALSE))

print(x)
# Create the data frame.
BMI <- data.frame(gender = c("Male", "Male","Female"),
                   height = c(152, 171.5, 165),
                   weight = c(81,93, 78),
                   Age = c(42,38,26))

print(BMI)<jupyter_output>gender height weight Age
1   Male  152.0     81  42
2   Male  171.5     93  38
3 Female  165.0     78  26
{'fraction_non_alphanumeric': 0.12915309446254072, 'fraction_numerical': 0.036482084690553744, 'mean_word_length': 3.5624071322436848, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '145531', 'license': 'MIT', 'n_tokens_mistral': 2154, 'n_tokens_neox': 1958, 'n_words': 800, 'path': 'Week 01/R_03_Data_Types.ipynb', 'repo_name': 'shirazipooya/RCourse', 'size': '6140'}