Commit ee61bb16 by Weigert, Andreas

### R Intro 1 solution added

parent 3e982601
 --- title: 'Tutorial 1: R Introduction 1' output: html_notebook editor_options: chunk_output_type: inline --- This file is part of the lecture Business Intelligence & Analytics (EESYS-BIA-M), Information Systems and Energy Efficient Systems, University of Bamberg. ```{r Exercise: Mathematical calculations} # Exercise 1 2 + 3*5^2 # Exercise 2 2^3^5 # Exercise 3 1.4e-2 # Exercise 4 1.4*exp(-2) # Exercise 5 3*exp(1)^5 + 2 # Exercise 6 log2(33554432) # Exercise 7 sin(pi/2) ``` ```{r Exercise: Objects and assignements} # Exercise 8 text <- c("a", "b") text # Exercise 9 row <- c(3, 7, 11, 15) row # Alternative: row <- seq(from=3, to=15, by = 4) row # Exercise 10 u <- 1:10 u # Exercise 11 x <- sample(1:99, 10) x ``` ```{r Exercise: Working with vectors} # Exercise 12 x[1:3] # Exercise 13 sort(x) # Exercise 14 order(x) # Exercise 15 x[order(x)] # Exercise 16 x[x%%2==0] # Exercise 17 v <- x^2 # Exercise 18 u <- u + 1 # Exercise 19 v/x # Exercise 20 sqrt(v) # Exercise 21 x + 0:1 ``` ```{r Exercise: Working with matrices} # Exercise 22 matrix(0,nrow=2,ncol=2) # Exercise 23 matrix(x,nrow=2) # Exercise 24 A <- matrix(x,nrow=2) # Exercise 25 A[1,] # Exercise 26 sum(A[,-2]) ``` ```{r Exercise: Working with lists} # Exercise 27 list_data <- list(text, u, x, A) # Exercise 28 list_data[[2]] # Exercise 29 list_data[[2]][3] # Exercise 30 names(list_data) <- c("text", "u", "x", "A") # Exercise 31 list_data # Exercise 32 list_data\$A ``` ```{r Exercise: Working with data frames} # For Exercise 33 ID <- c(876, 563, 345, 939) Name <- as.character(c("Michael", "Susan", "Hans", "Björn")) Age <- c(29,23,56,35) Nationality <- as.factor(c("DE","US","DE","SE")) # Exercise 33 students <- data.frame(ID=c(876, 563, 345, 939), Name=as.character(c("Michael", "Susan", "Hans", "Björn")), Age=c(29,23,56,35), Nationality=as.factor(c("DE","US","DE","SE"))) # Long command: If you do not use proper datatypes and enhance the data.frame command by "stringsAsFactors = F", data types might not be correct. students <- data.frame(ID=as.numeric(876, 563, 345, 939), Name=as.character(c("Michael", "Susan", "Hans", "Björn")), Age=as.numeric(c(29,23,56,35)), Nationality=as.factor(c("DE","US","DE","SE")), stringsAsFactors = F) # Exercise 34 View(students) # Exercise 35 students[3,2] # Exercise 36 students\$Age # Exercise 37 students[students\$Age < 30,"Name"] # colum name as identifier students[students\$Age < 30,2] # column index as identifier students[students\$Age < 30,]\$Name # get a data.frame and then select the variable by \$ operator ```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!