0% found this document useful (0 votes)
14 views19 pages

Module 2.5

The document provides an overview of data types and variables in programming, specifically in R. It explains various data types such as numeric, integer, character, logical, complex, raw, and their respective uses, along with how to create and manipulate variables. Additionally, it introduces R objects like vectors, lists, matrices, factors, arrays, and data frames, detailing their structures and functionalities through hands-on activities.

Uploaded by

Squall Lionheart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views19 pages

Module 2.5

The document provides an overview of data types and variables in programming, specifically in R. It explains various data types such as numeric, integer, character, logical, complex, raw, and their respective uses, along with how to create and manipulate variables. Additionally, it introduces R objects like vectors, lists, matrices, factors, arrays, and data frames, detailing their structures and functionalities through hands-on activities.

Uploaded by

Squall Lionheart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2.

5 DATA TYPES & VARIABLES

Ydataoutype.
already executed commands and used a variable and a
Now let’s learn more about these things better in
this topic.

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, oating point, double
oating 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.

Imagine data as a diverse group of people. Each person


might speak a di erent language – numbers, words, or even
a combination. Similarly, R recognizes di erent data types,
each suited to a speci c type of information:

• Numeric: These represent numbers, encompassing


both integers (whole numbers without decimals)
and doubles (numbers with decimals).
Example: Age (35), Income (12345.67)

• Integer: A speci c type of numeric data


representing whole numbers without decimals.
Example: Number of customers (100)

• Character: These represent text strings, allowing


you to store words, phrases, or even sentences.
Page 39 of 77
fl
ff
fi
fi
fl
ff
Example: Customer name ("John Smith”),
Product description ("Blue T-shirt”)

• Logical: These are the simplest data types,


representing only two values: TRUE or FALSE.
They’re perfect for indicating yes/no situations.
This is commonly known as BOOLEAN in the
programmer world.
Example: Is Active Customer (TRUE/FALSE)

• Complex: These represent complex numbers,


which combine a real and imaginary component.
They're less commonly used in data analysis but
are useful for speci c scienti c or engineering
applications.
Example: Electrical impedance (2 + 3i)

• Raw: This data type stores information in its most


basic form – bytes. It's rarely used directly in data
analysis but can be handy for working with low-
level data manipulation.

Now that you understand the di erent data "languages,"


it’s time to give them names! Variables in R act like
containers, holding speci c data values of a particular type
just like the myGreetings variable that you used from the
previous topic. Think of them as personalized name tags for
your data.

A variable provides us with named storage that our


programs can manipulate. A variable in R can store an atomic
vector, group of atomic vectors or a combination of many
Robjects. A valid variable name consists of letters, numbers
and the dot or underline characters. The variable name starts
with a letter or the dot not followed by a number.

Page 40 of 77
fi
fi
fi
ff
Variable Name Validity Reason

Has letters, numbers, dot and


var_name2. Valid
underscore

Has the character '%'. Only dot


var_name% Invalid
(.) and underscore allowed.

2var_name Invalid Starts with a number

Can start with a dot(.) but the


.var_name,
Valid dot (.) should not be followed
[Link]
by a number.

The starting dot is followed by


.2var_name Invalid
a number making it invalid.

_var_name Invalid Starts with _ which is not valid

Page 41 of 77
HANDS-ON ACTIVITY # 1

TSo,here is nothing better than doing the hands-on learning.


let’s start with a simple data entry activity that will use the
di erent data types presented above. All you need to do is to
run the R scripts to create variables that will describe you and
assign values to each of the variables.

Hint: Take a look at a biodata.

Write the R scripts (commands) you used on each line and have
it validated by your classmate or your instructor.

> name

>

Please use a separate sheet should your need more lines.


Page 42 of 77
ff
Along with the data types and the variable, R is also
implementing Data Object (similar to C, Java and Python
among others) that is commonly termed as R-Object. These
objects handled the data in a special manner which allows for
easy data store and handling process.

Now that you've grasped the di erent data types and


variables in R, it's time to explore the structures that hold and
organize them. These R objects act as containers, each
o ering unique ways to manage your data:

Vectors: Imagine a single- le line of people. Vectors in R


function similarly, storing a collection of elements of the same
data type. Think of them as ordered lists, where each element
has a speci c position (index). Here are some common vector
operations:

• Accessing elements: You can access speci c


elements using their index (e.g., vector[3] retrieves
the third element).

• Combining vectors: You can combine vectors of


the same data type using functions like c().

Example:

# Numeric vector of ages


ages <- c(35, 22, 48)

# Accessing the second element


second_age <- ages[2]

# Combining vectors
all_ages <- c(ages, 19)

Page 43 of 77
ff
fi
fi
ff
fi
• Lists: Unlike vectors, lists provide a more versatile
container. It can hold elements of di erent data
types, acting like a mixed bag. This makes them
ideal for storing diverse information related to a
single entity. Here are some key points about lists:

‣ Ordered elements: Elements in a list


maintain a speci c order, just like vectors.

‣ Accessing elements: You can access


elements using their index or names
assigned within the list.

Example:

# List containing customer information


customer_info <- list(
name = "John Smith",
age = 35,
active = TRUE
)

# Accessing element by name


customer_name <- customer_info$name

# Accessing element by index


second_element <- customer_info[[2]]

# Accesses age as it's the second element


customer_age <- customer_info[2]

Page 44 of 77
fi
ff
• Matrices: Think of a spreadsheet – that's
essentially what a matrix represents in R. It's a two-
dimensional structure where elements are
arranged in rows and columns. All elements must
be of the same data type. Matrices are useful for
organizing numerical data in a tabular format. Here
are some key points about matrices:

‣ Dimensions: Matrices are de ned by their


dimensions (rows x columns).

‣ Accessing elements: You can access


elements by specifying their row and
column index
(e.g., matrix[2, 1] retrieves the element at
row 2, column 1).

Example:

# Matrix representing customer purchases


purchases <- matrix(c(10, 5, 2, 8, 3, 1), nrow = 2, ncol = 3)

# Accessing element at row 2, column 1


second_row_first_col <- purchases[2, 1]

• Factors: Sometimes, your data represents


categories or groups. Factors in R allow you to
store these categorical variables e ciently. Under
the hood, they use numeric codes to represent
categories, but you can assign human-readable
labels for clarity.

Page 45 of 77
fi
ffi
Example:

# Factor representing customer status


customer_status <- factor(c("Active", "Inactive", "Active"))

# Levels and labels


levels(customer_status) #Shows the categories (Active, Inactive)
labels(customer_status) #Shows the assigned labels

• Arrays: While matrices are two-dimensional, arrays


can have more than two dimensions. Imagine a
stack of spreadsheets – that's the basic idea. It is
less commonly used in data analysis but are
valuable for speci c tasks involving
multidimensional data.

Example:

# Create a 3D array of dimensions (2 x 2 x 3)


# Filled with random numbers between 1 and 10
my_array <- array(runif(2*2*3, min=1, max=10), dim= c(2,2,3))

# Print the dimensions of the array


dim(my_array)
specific_element <- my_array[1, 2, 3]

# Access all elements in the first layer


first_layer <- my_array[, , 1]

# Access all elements in the second row


second_row <- my_array[2, , ]

Page 46 of 77
fi
• Data Frames: Data frames are arguably the most
powerful and commonly used data structure in R.
Imagine a table with rows and columns, but each
column can hold data of di erent types. This
exibility makes data frames ideal for storing and
manipulating diverse datasets used in data
analysis. Here are some key points about data
frames:

‣ Rows and columns: Data frames consist of


rows (observations) and columns (variables).

‣ Di erent data types: Each column can hold


a di erent data type (e.g., one column for
numeric ages, another or character names).

‣ Accessing elements: You can access


elements by row and column index or by
variable name.

Example:

# Data frame with customer data


customer_data <- [Link](
name = c("John Smith", "Jane Doe", "Mike Brown"),
age = c(35, 28, 42),
active = c(TRUE, FALSE,FALSE)
)

Page 47 of 77
fl
ff
ff
ff
HANDS-ON ACTIVITY # 2

Iprevious
t is now time to apply what you have learned from the
material. Please follow the instructions in order for
you to produce the desired output.

VECTORS:

Activity: Create a Movie Night!

Objective: Create vectors to store information about your


favorite movies.

Steps:
1. Create a numeric vector named movie_years to store the
release years of your top 3 favorite movies.

2. Create a character vector named movie_titles to store


the titles of your favorite movies.

3. C r e a t e a l o g i c a l v e c t o r ( b o o l e a n ) n a m e d
have_seen_recently to indicate if you’ve recently
rewatched each movie (TRUE) or not (FALSE).

4. Combine the vectors using the c() function to create a


single vector representing each movie with its details.5.
Use indexing to access speci c information about a
particular movie (e.g., print the release year of your
second favorite movie).

Page 48 of 77
fi
Write the R scripts (commands) you used on each line and
have it validated by your instructor.

>

Please use a separate sheet should your need more lines.


Page 49 of 77
LISTS:

Activity: Plan Your Dream Vacation!

Objective: Create a list to store information about your dream


vacation.

Steps:
1. Create a list named dream_vacation to store details about
your dream trip.

2. Add elements to the list with names like destination


(character), travel_duration (numeric - days), activities
(character vector with multiple activities), and
estimated_budget (numeric).

3. Access speci c elements of the list using their names


(e.g., print the estimated budget for your dream vacation).

4. Add a new element to the activities vector within the list to


include another activity you’d like to do.

Page 50 of 77
fi
Write the R scripts (commands) you used on each line and
have it validated by your instructor.

>

Please use a separate sheet should your need more lines.


Page 51 of 77
MATRICES::

Activity: Track Your Movie Ratings!

Objective: Create a matrix to store your ratings for di erent


movies across various genres.

Steps:
1. De ne a matrix named movie_ratings with rows
representing movies and columns representing genres
(e.g., Action, Comedy, Drama). Assign numeric values (1-5)
for your ratings.

2. Use the dim() function to check the dimensions of your


matrix.

3. Access speci c ratings using row and column indices


(e.g., print your rating for the second movie in the Comedy
genre).

4. Calculate the average rating for each genre


using matrix operations (e.g., rowMeans()).

Page 52 of 77
fi
fi
ff
Write the R scripts (commands) you used on each line and
have it validated by your instructor.

>

Please use a separate sheet should your need more lines.


Page 53 of 77
FACTORS:

Activity: Analyze Your Music Collection!

Objective: Create a factor to represent the genres of your


music collection.

Steps:
1. Create a character vector named music_genres
containing the genres of your favorite songs.

2. Convert the character vector to a factor named genres


using the factor() function.

3. Use the levels() and labels() functions to see the di erent


categories (genres) and their assigned labels (if any).

4. Count the number of songs in each genre using


the table() function.

Page 54 of 77
ff
Write the R scripts (commands) you used on each line and
have it validated by your instructor.

>

Please use a separate sheet should your need more lines.


Page 55 of 77
DATA FRAMES:

Activity: Build Your Book Collection!

Objective: Create a data frame to store information about


your favorite books.

Steps:
1. Create a data frame named book_collection with
columns for title (character), author (character), genre
(factor), year_published (numeric), and
number_of_pages (numeric).

2. Add rows to the data frame representing each book in


your collection.

3. Access speci c information about a particular book by


row index or column name (e.g., print the title and author
of the third book).

4. Sort the data frame by publication year using the


arrange() function from the dplyr package. You can as
your instructor regarding package installation. Also, you
can search more information in the Internet.

Page 56 of 77
fi
Write the R scripts (commands) you used on each line and
have it validated by your instructor.

>

Please use a separate sheet should your need more lines.


Page 57 of 77

You might also like