Presentation is loading. Please wait.

Presentation is loading. Please wait.

R course 4th lecture.

Similar presentations


Presentation on theme: "R course 4th lecture."— Presentation transcript:

1 R course 4th lecture

2 Topics we will cover Dataframes, Arrays, Factors, Lists, Transposing, Functions

3 Dataframe 2-dimensional, any type
df <- data.frame(name = c("Matt", "Joe", "Chris"), age = c(52, 29, 25), relationshipStatus = c("married", "single", "married"))

4 Task planets <- c("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune") type <- c("Terrestrial planet", "Terrestrial planet", "Terrestrial planet", "Terrestrial planet", "Gas giant", "Gas giant", "Gas giant", "Gas giant") diameter <- c(0.382, 0.949, 1, 0.532, , 9.449, 4.007, 3.883) rotation <- c(58.64, , 1, 1.03, 0.41, 0.43, -0.72, 0.67) rings <- c(FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE) Create a dataframe from these vectors Make sure that you've actually created a data frame with 8 observations and 5 variables with str()

5 Array n-dimensional, same type
a <- array(data=c(1:6), dim = c(2,2,2)) gives a 2x2x2 array

6 Factors Factors > blood <- c("B", "AB", "O", "A", "O", "O", "A", "B") > blood_factor <- factor(blood) [1] B AB O A O O A B Levels: A AB B O > str(blood_factor) Factor w/ 4 levels "A","AB","B","O": Can also do various things such as renaming levels and ordering

7 Lists - Used to store R objects with no coercion, but some loss of functionality
> song <- list("Rsome times", 190, 5) > names(song) <- c("title", "duration", "track") > song <- list(title = "Rsome times",duration = 190,track = 5) Extending lists friends <- c("Kurt", "Florence","Patti", "Dave") song$sent <- friends Can also subset lists, [ (incl. names) vs [[: song[1] vs song[[1]]

8 Transposing A very useful command is t()
This transposes data and can be very useful when manipulating data sets

9 An introduction to functions
Now we have looked at data structures like matrices and vectors, we can look at functions Functions are used to incorporate sets of instructions that you want to use repeatedly or that are easier to store so they can be called when needed A function is a piece of code written to carry out a specified task; it may take inputs and it may return one or more values. Functions are created using function() and are stored as R objects They print the last expression in the body

10 Functions Functions can be passed to other functions as arguments (can use a function as an input) Functions can be nested (can have a function within a function) 3 components to functions: The body(), which is the code inside the function. The formals(), which is the list of argument of the function. The environment(), which is the map of the location of the function’s variables. f <- function(<arguments>){ # function body }

11 Function example square<-function(x) { x^2 }

12 add<-function(x,y){
}

13 Nesting functions power <- function(exponent) { function(x) { x ^ exponent } } cube <- power(3) cube(2) Or can just do power(3)(2)

14 Task Create a function modulus which returns x modulo y

15 Creating more advanced functions
We can use control structures in functions such as: If Else For While

16 If if(<condition>){ # Do something if the condition is true }
else{ # Do something if the condition is false

17 if(<condition1>){
# Do something if the condition1 is true } else if(<condition2>){ # Do something if the condition1 is false and condition2 is true } else if(<condition3>){ # Do something if the condition1 is false and condition3 is true } else{ # Do something if none of the conditions are true }

18 if(number > 0){ print("The number is greater than 0") } else if(number > 10){ print("The number is greater than 10") } else{ print("The number is less or equal to 0") }

19 For loop The for loop allows code to be executed repeatedly for a given iteration for(<variable> in <sequence>){ # Do something } for(i in 1:5){ print(i^2) }

20 While loop While loops test if a condition is true, and if it is then the following body of code is executed, and then it runs again, until the condition is false, at which point it stops. while(<condition>){ # Do something } You need to be careful not to create infinite loops!

21 i <- 0 while(i < 15){ i <- i+ 3 print(i) }

22 Using the control structures in functions
positive<-function(x){ if(x<0){ -x } else { x }}

23 f <- function(x){ if(x > 0){ "The number is greater than 0" } else{ "The number is not greater than 0" }

24 Squarelist <- function(a) { for(i in 1:a) { b <- i^2 print(b) } }

25 Task Create a function which finds the area of a circle given a radius r Note: pi is already a variable stored in R

26 Task Create a function which removes even numbers from a vector
Hint: Use modulus and vector subsetting

27 There are several solutions…
Easiest solution is just removeeven<- function(x){ x[x%%2 != 0] }

28 Can also use while and if
removeeven<-function(x){ i<-length(x) while(i>0){ if(x[i]%%2==0){ x<-x[-i] } i<-i-1 x

29 Could also use ceiling instead of modulus
removeeven<-function(x){ i<-length(x) while(i>0){ if(ceiling(x[i]/2)==x[i]/2){ x<-x[-i] } i<-i-1 x

30 Or even simpler… removeeven<- function(x){ x[ceiling(x/2)!=x/2] }


Download ppt "R course 4th lecture."

Similar presentations


Ads by Google