# scalar q <- 3 w <- 17 e <- q * w e e <- w / q e log(exp(sin(pi/4)^2) * exp(cos(pi/4)^2)) # vectors qq <- c( 1:10 ) qq <- 1:10 ww <- runif( 10 ) ee <- qq * ww ee ee <- qq / ww ee qqq <- qq[2] qqq qqq <- qq[2:4] qqq qqq <- qq[c( 1:3, 7 )] length(qqq) # matrices Q <- cbind( qq, ww ) Q dim( Q ) Q <- rbind( qq, ww ) Q dim( Q ) Q <- t( Q ) Q dim( Q ) W <- cbind( ww, qq ) W WW <- W[2, 1] WW WW <- W[, 1] WW WW <- W[2, ] WW WWW <- t( W ) # scalar multiplication QQ = Q * 3 QQ # element by element E <- Q * W E # multiplication Q %*% W large <- Q %*% t( W ) large small <- t( Q )%*% W small # inverse small_i <- solve( small ) small_i # Identity small_i %*% small # example xvalues <- 1:200 yvalues <- 17 + 2 * xvalues + rnorm( 200, sd = 10 ) # plotting plot( x = xvalues, y = yvalues, col = "blue", type = "p" ) lines( x = xvalues, y = 17 + 2 * xvalues, col = "green", lw = 5 ) # OLS X <- cbind( rep( 1, 200 ), xvalues ) solve( ( t( X )%*% X ))%*% t( X )%*% yvalues # check with internal formula summary( lm( yvalues ~ xvalues )) ######################## # functions # descriptive statistics sd( ww ) mean( ww ) min( ww ) max( ww ) sd( WW ) mean( WW ) min( WW ) max( WW ) sd( W[1, ] ) mean( W[, 2] ) min( W[2:3, ] ) max( W[c( 1, 3 ), ] ) descr_stats <- function( INFRATRAIN ) { c( sd( INFRATRAIN ), mean( INFRATRAIN ), min( INFRATRAIN ), max( INFRATRAIN ) ) } descr_stats( ww ) descr_stats <- function( INFRATRAIN ) { t1 <- c( sd( INFRATRAIN ), mean( INFRATRAIN ), min( INFRATRAIN ), max( INFRATRAIN )) t2 <- as.matrix( t1 ) rownames( t2 )<- c( "sd", "mean", "min", "max" ) t2 } descr_stats( ww ) ww_descr <- descr_stats( ww ) # call the function from the other descr_stats_row <- function( INFRATRAIN ) { t1 <- descr_stats( descr_stats( ww )) t( t1 ) } descr_stats_row( ww ) # rounding round( ww_descr, digits = 3 ) ################### # programming rr <- rnorm( 1, mean = 10 ) rr if ( rr >= 0 ){ print( "My random number is greater or equal 0." )} else { print( "My random number is smaller than 0." ) } # ifelse ifelse( ww > 0.5, "larger", "smaller" ) # conditions rrr <- rnorm( 1, mean = -10 ) rrr rr > 0 & rrr > 0 rr > 0 | rrr > 0 ## Repetitive Execution countries <- c( "Austria", "Belgium", "Germany", "Netherlands", "United Kingdom", "United States", "Zimbabwe" ) for ( i in countries ){ print( i ) } for ( i in 1:5 ){ print( countries[i] ) } for ( i in 1:5 ){ cat( " Country is: ", countries[i], "\n" ) } # APPLY instead of for-loop # apply( myobject, in.which.direction, which.function ) apply( Q, MARGIN = 1, mean ) apply( Q, MARGIN = 1, descr_stats ) apply( t( Q ), MARGIN = 2, mean ) # folder specification setwd("~/research/data") getwd() # import / export mm <- 17 x1 <- sample( mm ) x2 <- sample( mm ) x3 <- sample( mm ) x4 <- sample( mm ) x5 <- sample( mm ) write.table( cbind( x1, x2, x3, x4, x5 ), file = "x.dat", sep = ", ", row.names = F, col.names = T ) y <- rep( 1, mm ) y[sample( mm, 10 )] <- -1 write.table( y, file = "y.dat", sep = ", ", row.names = F, col.names = T ) q_data <- read.csv( "x.dat", header = F ) # paste paste( "",getwd(),"/x.dat", sep = "" ) # read the data dat <- read.csv( paste( "",getwd(),"/x.dat", sep = "" ), header = T, sep = "," ) dat # sorting and other data manupulations data1 = dat[order( dat$x1 ), ] data1 n <- nrow( dat ) data2 <- matrix( c( dat$x2 ), nrow = n, ncol = 1 ) data2 data3 <- matrix( c( dat$x2, dat$x3, dat$x4 ), nrow = n, ncol = 3 ) data3