Data Types: data classes or types are the ways in which we categorize objects in R. Data classes are basically storage for the type of objects or variables that we need when manipulating data. For example, lets say we are importing a data set about the number of childs per household, we are storing those objects as numeric data.Another example, lets say we are working with a data set about the hair style of people in Boston College, those objetcs are stored as characters since they are a text variable.
Numeric: it represents all real numbers, including decimals.
Logical: it is data that only has two values, True or False.
integer: specifies real values without decimals.
complex: it specifies imaginary values.
character: it specifies text values in a variable.
raw: is just variables that are in raw code.
Data Structures: data classes are the ways in which we store objects or variables. Data structures are the ways in which we decide to organize those objects or variables.We can arrange our numeric values in vectors or matrices. We can arrange our characters into a list. Data frames usually contain multiple data types since they are a collection of vectors.
vector: Is a collection of data in a 1 dimension array. The data is arranged in a straight line. The data that contains is all of the same type.
matrix: matrix is the same as a vector but in a 2 dimension array, meaning that it has rows and columns. All data contained is of the same type.
data frame: I s a collection of vectors in a 2 dimension array. It can have vectors of different data types but all must be of the same length.
list: A list in an ordered collection of objects. It can have contain any data type, any length or any structure, it can even hold more lists.
table: It is very similar to a data frame. They are often used to display and organize data.
EU <- EuStockMarkets
class(EU)
## [1] "mts" "ts" "matrix" "array"
The function class() tells us how is the data arranged. In this case, the data is in a matrix. It also shows that the data is a time series “ts” and that inside the matrix, it is organized in array. This function shows the data structure.
typeof(EU)
## [1] "double"
The function typeof() tells us that the objects in the matrix are numbers with up to two decimals. This function shows the data type.
vector1 <- c(1,7,6,10,22,8,12)
R_StandardDeviation_InBuilt <- sd(vector1)
R_StandardDeviation_InBuilt
## [1] 6.528327
R_StandardDeviation_Hand <- sqrt(sum((vector1 - mean(vector1))^2 ) / (length(vector1)-1))
R_StandardDeviation_Hand
## [1] 6.528327
sd
## function (x, na.rm = FALSE)
## sqrt(var(if (is.vector(x) || is.factor(x)) x else as.double(x),
## na.rm = na.rm))
## <bytecode: 0x000001e8b5e31c78>
## <environment: namespace:stats>
This function is used to calculate the standard deviation. Despite using it previously, I can see in the code of the function that it performs the square root of the variance which is the standard deviation
IQR
## function (x, na.rm = FALSE, type = 7)
## diff(quantile(as.numeric(x), c(0.25, 0.75), na.rm = na.rm, names = FALSE,
## type = type))
## <bytecode: 0x000001e8b6cfa398>
## <environment: namespace:stats>
This function is used to calculate the interquartile range. I can identify in the code that it is a difference between the objects that sit at the .75 and .25
mean1 <- function(vector){
mean <- sum(vector)/length(vector)
return(c(mean))
}
mean1(vector1)
## [1] 9.428571
mean(vector1)
## [1] 9.428571
std<- function(vector){
std <- sqrt(sum((vector1 - mean(vector1))^2 ) / (length(vector1)-1))
return(c(std))
}
std(vector1)
## [1] 6.528327
sd(vector1)
## [1] 6.528327
data(rock)
plot(x = density(rock$area),
main = "Area of pores space",
ylab = "Frequency"
)
-From the graph, I can observe that there is a negative skewness. In this graph, skewness seems medium to low so it might be a mild problem.
plot(x = density(rock$peri),
main = "Perimeter",
ylab = "Frequency"
)
-From this graph I can observe that there is no skewness, the data seems symmetrical. Skewness is not a problem
plot(x = density(rock$shape),
main = "Area",
ylab = "Frequency"
)
- From this graph I can observe that this variable has a positive skewness. It seems like skewness is a problem for this variable
plot(x = density(rock$perm),
main = "Permeability",
ylab = "Frequency"
)
- From this graph I can observe that it has a positive skewness. It looks like skewness is a problem in the data
skewness(rock)
## area peri shape perm
## -0.30389730 0.02501158 1.16932766 0.71571258
Now we can compare with my previous analysis made from observing the graphs. A skewness value between 0.5 and -0.5, skewness represents no problem for the data. A skewness value between 0.5 and 1 or between -0.5 and -1, skewness represents a moderate problem for the data. If the skewness values are greater than 1 or lower than -1, then skewness is a big problem for the data.
The peri variable has a positive skewness of 0.025 which makes skewness not a problem for this variable
The shape variable has a positive skewness of 1.169, which makes skewness a problem for this variable
The perm variable has a positive skewness of 0.716, which makes skewness a moderate problem for this variable