Data Analysis Using R and Vectors
Data Analysis Using R and Vectors
x <- 10
y = 20
a <- 5
b <- 8
c <- a + b
print(x)
print(y)
print(c)
output
[1] 10
[1] 20
[1] 13
This output indicates that x is 10, y is 20, and c (which is the sum of a
and b) is 13. The [1] indicates the index of the outpu
df <- data.frame(
return(a + b)
}
Attributes of objects
In R, objects can have attributes associated with them. Attributes
provide additional metadata about the object.
Names: Many objects in R, such as vectors, lists, and data frames, can
have names associated with their elements. You can assign names using
the names() function.
Class: The class attribute specifies the type of object. Many functions in
R use the class attribute to determine how to handle objects. You can
set or modify the class using the class() function.
# Creating a numeric vector and setting its class to "myclass"
Example:
str(df)
Output:
'data.frame': 3 obs. of 3 variables:
$ Age : num 25 30 35
summary(df)
Output:
Name Age Married
Mean :30.00
3rd Qu.:32.50
Max. :35.00
The workspace is stored in memory and persists until you end your R
session or explicitly remove objects from it. You can save the entire
workspace or specific objects within it to a file using functions like
save() and saveRDS(), and later load them back into R using functions
like load() and readRDS().
print(seq1)
Output:
[1] 1 2 3 4 5 6 7 8 9 10
print(seq2)
Output:
1] 1 3 5 7 9
Specifying Length: Instead of specifying the end point, you can specify
the length of the sequence using the length.out parameter.
print(seq3)
Output:
print(seq4)
Output:
[1] 2 4 6 8 10 12 14 16 18 20
- Addition +
- Subtraction -
- Multiplication *
- Division /
- Exponentiation ^ or **
- Modulo (remainder) %%
- Assignment <- or =
- Equal to ==
- Not equal to !=
- OR | or ||
- NOT !
2. Statistical Analysis:
3. Machine Learning:
4. Bioinformatics:
6. Geospatial Analysis:
7. Text Mining:
8. Web Scraping:
Writing R Code:
1. Write your R code in the script file just like you would in the R
console.
2. You can include comments in your script using the # symbol.
Comments are ignored by R and are used to add explanatory
notes to your code.
a <- 5
b <- 10
sum <- a + b
[1] 15
source("example_script.R")
Working Directory:
By default, R looks for script files in the current working directory. You
can use the setwd() function to change the working directory if needed.
You can also specify the full path to the script file when using the
source() function.
setwd("path/to/your/directory")
source("path/to/your/script/example_script.R")
Using script files is a convenient way to organize and execute your R
code, especially for larger projects or when you want to reuse code
snippets. It also helps in documenting your analysis and sharing your
work with others
Creating Vectors: You can create a vector in R using the c() function,
which stands for "combine" or "concatenate":
# Accessing elements
# Vector addition
o Sorting a vector
Vector Length and Type: You can find out the length of a vector
using the length() function, and the data type using the typeof()
function:
o Length of vector
o Modifying elements
Example:
Example:
integer_vector <- c(1L, 2L, 3L, 4L, 5L)
Example:
Example:
Example:
Example:
output:
print(my_vector)
print(my_vector)
print(my_vector)
output:
print(result_add) # Output: 5 7 9
print(result_mul) # Output: 4 10 18
output:
print(length_my_vector) # Output: 5
print(sorted_vector)
print(unique_elements)
# result: [5, 7, 9]
# result: [3, 6, 9]
Output: [1] 25
Example:
Output: [1] 25
Example:
library(modeest)
Output: [1] 10 15 20 25 30 35 40
Output:[1] 10 40
5. Quartiles: Values that divide a set of numbers into four equal parts.
The first quartile (Q1) represents the 25th percentile, the second
quartile (Q2) is the median (50th percentile), and the third quartile (Q3)
is the 75th percentile.
Example:
Example:
Output:[1] 11.18034
Comparing vectors in R:
In R, you can compare vectors using relational operators, which return
logical values (TRUE or FALSE). Here are the common relational
operators for comparing vectors:
1. *Equality*: ==
vector1 == vector2
2. *Inequality*: !=
vector1 != vector2
Example:
print(greater_than)
print(less_than)
print(equal_to)
print(not_equal_to)
print(greater_than_or_equal_to)
print(less_than_or_equal_to)
Output:
# sorted_vector: [1, 2, 3, 4, 5]
2. *Descending Order*: You can specify decreasing = TRUE to sort the
vector in descending order.
# sorted_vector_desc: [5, 4, 3, 2, 1]
# sorted_indexes: [2, 4, 1, 3, 5]
# sorted_vector_by_index: [1, 2, 3, 4, 5]
# first_name: "Alice"
# a_names: ["Alice"]