Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Practices - 1
#R version 3.3.2 # Numbers and Vectors Manipulation # c(1, 2, 3) is a vector # x = c(1, 2) and then y = c(x, 0, x) # then y will be c(1, 2, 0, 1, 2) # if x is a vector, then min, max, mean, # length all work on x and return one value # sum, prod as well # Note when all scalar functions above work on # multiple variables, they all return one value # e.g.: x = c(1, 2, 3), y = c(4, 5, 6), z = c(7, 8, 9); # and max(x, y, z) -> 10; # Sequence # seq(from = a, to = b) or seq(start, end, by=steps); # or seq(length = l, from = a, by = steps) # and this will return a vector # rep(x, times = b) will replicate x for n times # If x is a vector, then rep(x, each = b) will replicate # each element in x separately for b times. # e.g.: x = c(1, 2) then rep(x, each=3) -> c(1, 1, 1, 2, 2, 2); # Logicla operators: &, ||, !... # NA means missing values and NaN only works for missing numbers # is.na(x) will tell is x is NA or not and same for is.nan(x); # Character vectors: \n: newline, \t: tab # paste(c(str_1, str_2, ...), start:end, sep = string); # e.g.: paste(c('a', 'b'), 1:5, sep = 'cc')); # -> c('acc1', 'bcc2', 'acc3', 'bcc4', 'acc5'); # if x is a vector, then x[conditions applied on element on x] # will remain elements satisfying that condition # e.g: x = c(1, 2, 3); y = x[x >= 2] and y = c(2, 3); # NOTE: in vectors, index start from 1 #y = c('x', 'y')[rep(c(1, 2, 2, 1), times = 4)]; #print (y); # In this case the vectors in rep can provide the format # to replicate x and y. # Here numbers will represent the index of the vector in the front # x[-(1:5)] means the vectors without the value from index 1 to 5 within x # And abs(x) will take the abs value of all the value in x x = c('a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'r', 'e'); y = c(1, 2, 3, 1, 2, 9, 4, 4, 6, 2); z = tapply(y, x, mean); # To use the tapply function, we need list(a vector) of # strings which stand for dimension, and then its level # will tell you all unique value # tapply (data, dimension, calculation(including mean, ..)) # Then the result will be a table including data broken # by the level of the factor x = c(1, 2, 3, 1, 2, 3, 1, 2, 4, 1, 4, 4); dim(x) = c(2, 3, 2); # When assigning c(a, b, c) to dim(v) where v is a long vector # when print v, it will break down v into c matrixes with a rows and b columns # Array target # v = array(start_1:end_1, start_2:end_2, ..., dim = c(numof_rows, numof_cols)); # without the dim, then v will be printed in one long line #e.g.: x = array(0, dim = c(3, 4, 1)) # produce 2 3x4 matrixed with all 0 y = array(c(1:8, 2:5), dim = c(4, 2, 4)); # Outer product between two vectors: # ab = a %o% b or ab = outer (a, b, "*") f = function(x, y) cos(y)/(1 + x^2); # a function with input x, y and output that formula a = c(1, 2, 3); b = c(2, 3, 4); v = outer(a, b, "*"); # Here outer product means a times the transpose of b # and inner product means the transpose of b times b #d = outer(0:9, 0:9); #fr = table(outer(d, d, "-")); #plot (as.numeric (names(fr)), fr, type = "h", # xlab = "Determinant", ylab = "Frequency"); # if A is a matrix, then t(A) will return the transpose of A # and nrow(A) and ncol(A) will return the number of rows and columns of A # and %*% is the matrix multiplication # Warning: when multiplying vectors, try not to use %*% # Solving linear equations and inversion # A a matrix, x a vector # b = A %*% x and then solve(A, b) can return x. Here x do not need to be given # Eigenvalue/Eigenvectors # Note that this can only be applied to a symmetric matrix S = array(c(1:3), c(2, 2));
run
|
edit
|
history
|
help
0
ExDecaiEulerMin28-01-21
5 # SUMMARY PART A
Exp 2
Zerlina L Bi-variate Regression
Gab1B-nb
Chev-nodes
Correlação
time series
simple program
rungeefeito