Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############ UE 2 ############
- library(tidyverse)
- install.packages("tidyverse")
- ##1.)
- #a)
- float<-3.0
- newfloat<-float
- float<-1.0
- float
- newfloat
- #newfloat still has its original value
- #b)
- string<-"hello"
- newString<-string
- string<-"world"
- newString
- string
- #same as for a
- #e)
- list1<-list(1:3)
- list2<-list1
- list1<-list(1:4)
- list2
- list1
- #R uses pass-by-value semantics
- ##2.)
- #a)
- keys<-list("a","a","a","a","a","a","a")
- seq<-c(1:length(keys))
- names(seq)=keys
- #b)
- seq<-replace(seq,,NA) #compiler warning can be ignored :^)
- seq
- #c)
- seq[1]="Cool"
- seq[2]="Stuff"
- replace(seq,1,"Boring")
- #d)
- seq[which(!is.na(seq))] # to get the index and the name
- #e)
- givenKey<-"Cool"
- givenKey %in% seq
- #f)
- #1. Dictionaries in python musst always have a key value pair.
- #In R the key can be NA
- #2. In dictionaries the key must always be unique
- #3.
- ##4.)
- #a)
- number<-floor(runif(1,1,10))
- number
- #b)
- print("make your guess")
- #c)
- guess<-readline("Enter your number: ")
- guess
- #d)
- guess<-as.integer(guess)
- if(is.na(guess)){
- print("Wrong type. Please provide a number")
- } else {
- paste0("My number: ",number,". My guess: ",guess)
- if(guess==number) {
- print("You won. Gz")
- }else{
- print("loser.")
- }
- }
- ##5.)
- myFunc<- function(withNA=TRUE,...){
- args<-list(...)
- numberInput<-sapply(args,simplify2array)
- #a)
- paste("Number of arguments: ", length(args))
- #b)
- paste("Min: ",min(numberInput,na.rm = withNA)," Max: ",max(numberInput,na.rm = withNA))
- #c)
- paste("Sum of arugments:", sum(numberInput,na.rm = withNA))
- #d)
- paste("Mean of arguemnts: ", mean(numberInput,na.rm = withNA))
- }
- myFunc(withNA=TRUE,1,2,3,4,5)
- ##7.)
- target_temp_1 <- c(21.9485903842751, 22.0784102698089, 21.9961920387344, 22.0888801363402,
- 21.7593642619783, 22.0733746321091, 20.0650945334316, 22.2239271878111,
- 22.0009439997692, 21.9857914565644)
- target_temp_2 <- c(-4.86905373694365, -4.80753142788851, -5.01604866437737, -5.04518643042135,
- -4.7522254125673, -5.14586954359958, -6.94227853304683, -5.00661640454364,
- -4.99321895338312, -5.32219568456159)
- target_temp_3 <- c(9.96164369257947, 10.2791653162905, 10.0121926692844, 10.0902680418902,
- 10.082372143524, 10.2616048271993, 7.97374718604113, 9.70072495469742,
- 9.99002115411479, 9.72302091276975)
- target_temp_4 <- c(39.1663079568986, 39.1015313287077, 39.0140282858949, 38.9695153382043,
- 39.1154016526277, 38.997730939927, 37.0125229951796, 38.8548037473053,
- 38.9950516925261, 38.9622025227075)
- #a)
- temp1<-mean(target_temp_1)
- temp1
- temp2<-mean(target_temp_2)
- temp2
- temp3<-mean(target_temp_3)
- temp3
- temp4<-mean(target_temp_4)
- temp4
- #b)
- absoluteDeviation<-function(x){
- return(x-temp1)
- }
- lapply(target_temp_1,absoluteDeviation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement