In: Advanced Math
Assignment 1 - IN PDF FORMAT Using R and Rstudio
Pick a database from: data()
Then preview the first 10 rows.
Print the number of rows and columns
- Print the names of the variables
If you have row names, print them - work with the values for a field in your dataset. You can do it by dataset[[xx]] operator with xx can be the index of the field or the nae of the field.
Now use dataset[xx] to get a slice of datframe instead of vector, e.g. df = mtcars[c(‘mpg’, ‘disp’)] - With two scalar arguments (rows,columns), the [] operator can select elements in the dataframe. Try this.
- Subset your data using rules similar to the lectures. For example part of the data that has a characteristics AND/OR another one, etc.
- Write a function that does an operation, for example averaging a property, on your dataset.
- Write a function that works on rows and adds a column to the dataset with the results for each row.
- Using the iris database, explain the following line of code:
data(iris) aggregate(iris["Petal.Length"], by = iris["Species"], FUN=median)
## Species Petal.Length
## 1 setosa 1.50
## 2 versicolor 4.35
## 3 virginica 5.55
IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE TTHERE TO HELP YOU..ALL THE BEST
CODE:
set.seed(1) dataset <- data.frame(A = sample(c(NA, 1:100), 1000, rep = TRUE), B = rnorm(1000)) > head(dataset) A B 1 26 0.07730312 2 37 -0.29686864 3 57 -1.18324224 4 91 0.01129269 5 20 0.99160104 6 90 1.59396745
> nrow(dataset) [1] 1000 > NROW(dataset) [1] 1000
#1
rm(list = ls()) # 1.1 Modes and Classes mylist <- list(a = c(1, 2, 3), b = c("cat", "dog", "duck"), d = factor("a", "b", "a")) sapply(mylist, mode) sapply(mylist, class) # 1.2 Data Storage in R x <- c(1, 2, 5, 10) x mode(x) y <- c(1, 2, "cat", 3) mode(y) z <- c(5, TRUE, 3, 7) mode(z) all <- c(x, y, z) all x <- c(one = 1, two = 2, three = 3) x x <- c(1, 2, 3) x names(x) <- c('one', 'two', 'three') x str(x) mode(x) class(x) nums <- 1:10 nums + 1 nums + c(1, 2) nums + 1:2 nums + c(1, 2, 3) rmat <- matrix(rnorm(15), 5, 3, dimnames = list(NULL, c('A', 'B', 'C')) ) rmat rmat[, 'A'] as.matrix(rmat[, 'A']) mylist <- list(c(1, 4, 6), "dog", 3, "cat", TRUE, c(9, 10, 11) ) mylist sapply(mylist, mode) sapply(mylist, class) mylist <- list(first = c(1, 3, 5), second = c('one', 'three', 'five'), third = 'end') mylist mylist['third'] mylist <- list(c(1, 3, 5), c('one', 'three', 'five'), 'end') names(mylist) <- c('first', 'second', 'third') mylist # 1.3 Testing for Modes and Classes # no code # 1.4 Structure of R Objects mylist <- list(a = c(1, 2, 3), b = c('cat', 'dog', 'duck'), d = factor('a', 'b', 'a') ) summary(mylist) nestlist <- list(a = list(matrix(rnorm(10), 5, 2), val = 3), b = list(sample(letters, 10), values = runif(5)), c = list(list(1:10, 1:20), list(1:5, 1:10)) ) summary(nestlist) str(nestlist) list(1:4, 1:5) # 1.5 Conversion of Lists nums <- c(12, 10, 8, 12, 10, 12, 8, 10, 12, 8) tt <- table(nums) tt names(tt) sum(names(tt) * tt) sum(as.numeric(names(tt)) * tt) as.numeric("123") x=c(1, 2, 3, 4, 5) list(x) as.list(x)
I HOPE YOU UNDERSTAND..
PLS RATE THUMBS UP..ITS HELPS ME ALOT..
THANK YOU...!!