0% found this document useful (0 votes)
9 views2 pages

R Programming Basics for Data Analysis

This document outlines a tutorial on basic R programming for data analysis, covering fundamental math operations, variable assignment, and data type checks. It includes exercises for defining vectors, calculating total marks, and determining pass criteria based on marks. Additionally, it provides take-home exercises to reinforce learning through function applications and naming conventions.

Uploaded by

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

R Programming Basics for Data Analysis

This document outlines a tutorial on basic R programming for data analysis, covering fundamental math operations, variable assignment, and data type checks. It includes exercises for defining vectors, calculating total marks, and determining pass criteria based on marks. Additionally, it provides take-home exercises to reinforce learning through function applications and naming conventions.

Uploaded by

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

CT127-3-2 Programming for Data Analysis Basics of R

Tutorial 2- Basics of R

1) Try the basic math commands by typing the following expressions in the script
5+17
10-7
4*3*5
18/9
9%%4
(5*8)+(12-7)

2) Try defining/assigning values/ removing variables:


i. Define a new variable, weeks, then assign 4 to it using the operator (<-).
ii. Define a new variable, hoursPerWeek, then assign 40 to it using the operator
(=).
iii. Define a new variable, hourlyRate, then assign 50 to it using assign function.
i. Define a new variable, x, then assign 100 to it using any assignment operator.
Then, remove it using rm function.

3) Define new variables with different data types. Then, check the data type using
class(), [Link](), [Link] (), [Link](), and
[Link]() functions.

4) Define a vector, test_Marks, which contains the marks for 5 students in a test.
Then, define another vector, final_Marks, to store the marks for the 5 students in
the final exam. Test marks must be any value in the range 0-30, while final exam marks
must be any value in the range 0-70. Calculate and print the total marks for each student
(total_Marks).
5) Define a new vector, pass, that contains a logical value TRUE if the student’s total
mark, which was calculated in Exercise 4, is greater than or equal to 50.

6) Find the minimum, maximum, mean, and median values of total_Marks vector
using min, max, mean, and median functions, respectively.

Level-2 Asia Pacific University of Technology and Innovation Page 1 of 2


CT127-3-2 Programming for Data Analysis Basics of R

Take home exercises:


1) Give names to each final mark in total_Marks to represent students’ names by using
names function.
2) Try applying these functions:
round(547.8)
length(total_Marks)
sqrt(9)
substr(“abcd”, 2,4)
strsplit("hello world", " ")
paste("welcome to”, “PFDA")
nchar(“hello”)

Level-2 Asia Pacific University of Technology and Innovation Page 2 of 2

Common questions

Powered by AI

In R, elements in a vector can be named using the names() function, which assigns a name to each element. For example, if you have a vector total_Marks, you can assign student names as follows: names(total_Marks) = c("Alice", "Bob", "Charlie", ...). Naming elements is significant as it allows for easier referencing and interpretation of data, especially when handling large datasets where indices alone may be confusing or insufficient for context.

Numeric functions like round(), length(), and sqrt() can significantly aid data processing. round(547.8) provides rounded numbers, facilitating comparison and presentation of numeric data where precision can be left flexible. The length() function gives the number of elements in a vector, crucial for understanding data size and iterating operations. The sqrt() function calculates square roots, necessary in various mathematical computations like variance assessments, optimizing, and transformation functions. Together, they support data processing by providing essential numerical operations.

In R, you can define and assign values to variables using different assignment operators. For example, you can assign the value 4 to a variable called 'weeks' using '<-', like this: weeks <- 4. You can also use the '=' operator, such as: hoursPerWeek = 40. Additionally, the assign function can be used, like this: assign("hourlyRate", 50). You can remove a variable using the rm function, such as rm(x)

Using different assignment operators in R, such as '<-', '=', and assign(), can impact both readability and functionality. The '<-' operator is the most idiomatic and widely recognized in R, often considered clearer and easier to read. The '=' operator can lead to confusion in contexts where it might also be interpreted as a comparison or assignment expression contextually more suitable in scripting. The assign() function, while useful for dynamic assignment, can complicate the code by separating the name and value assignment visually, potentially reducing immediate readability.

To calculate total marks for students in R, you first define vectors for test and final exam marks, such as test_Marks and final_Marks. The sum for each student can be computed by adding the elements of these vectors to get total_Marks: total_Marks = test_Marks + final_Marks. A vector 'pass' can then be created to contain TRUE if a student's total_Mark is greater than or equal to 50, such as pass = total_Marks >= 50.

In R, conditional logic can be applied using logical operators and control structures to sort students based on their total marks. You can create logical vectors using conditions such as total_Marks >= 50 to determine which students have passed. This vector can be used with the order() or sort() functions to organize the total_Marks vector in a desired order, such as ascending or descending to facilitate ranking or categorization of student performance.

In R, you can use functions like min(), max(), mean(), and median() to analyze a vector of student marks. min() and max() identify the minimum and maximum marks, providing insights into the range of scores. mean() calculates the average mark, offering a measure of central tendency, while median() provides the middle score in the dataset. Together, these statistics help to summarize and understand the distribution and variability of student performance.

In R, you can determine data types using functions like class(), is.numeric(), is.integer(), is.character(), and is.logical(). These functions help to check whether a variable is of a particular data type. Understanding the data type is important because it affects how the data can be manipulated and analyzed. For instance, numeric operations cannot be directly applied to character types without conversion.

Key string manipulation functions in R include substr() for extracting substrings, strsplit() for splitting strings, paste() for concatenating strings, and nchar() for counting characters. These functions enhance text data handling by allowing for precise control over text data. For instance, substr("abcd", 2, 4) returns "bcd", helpful in extracting specific content. strsplit("hello world", " ") splits text into words. These capabilities are critical in data cleaning and transformation tasks.

The median value, which indicates the middle point of a data set, often provides more insightful analysis than the mean in marks distribution because it is not skewed by outliers. While the mean is influenced by exceptionally high or low values, making it less representative of a typical mark, the median offers a better central location of the data, especially in skewed distributions or those with outliers. This makes the median a robust measure for central tendency in such contexts.

You might also like