Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. ############ UE 2 ############
  2. library(tidyverse)
  3. install.packages("tidyverse")
  4. ##1.)
  5. #a)
  6. float<-3.0
  7. newfloat<-float
  8. float<-1.0
  9. float
  10. newfloat
  11.  
  12. #newfloat still has its original value
  13. #b)
  14. string<-"hello"
  15. newString<-string
  16. string<-"world"
  17. newString
  18. string
  19. #same as for a
  20. #e)
  21. list1<-list(1:3)
  22. list2<-list1
  23. list1<-list(1:4)
  24. list2
  25. list1
  26. #R uses pass-by-value semantics
  27.  
  28. ##2.)
  29. #a)
  30. keys<-list("a","a","a","a","a","a","a")
  31. seq<-c(1:length(keys))
  32. names(seq)=keys
  33.  
  34. #b)
  35. seq<-replace(seq,,NA) #compiler warning can be ignored :^)
  36. seq
  37.  
  38. #c)
  39. seq[1]="Cool"
  40. seq[2]="Stuff"
  41. replace(seq,1,"Boring")
  42.  
  43.  
  44. #d)
  45. seq[which(!is.na(seq))] # to get the index and the name
  46.  
  47. #e)
  48. givenKey<-"Cool"
  49. givenKey %in% seq
  50.  
  51. #f)
  52. #1. Dictionaries in python musst always have a key value pair.
  53. #In R the key can be NA
  54. #2. In dictionaries the key must always be unique
  55. #3.
  56.  
  57.  
  58. ##4.)
  59. #a)
  60. number<-floor(runif(1,1,10))
  61. number
  62. #b)
  63. print("make your guess")
  64. #c)
  65. guess<-readline("Enter your number: ")
  66. guess
  67. #d)
  68. guess<-as.integer(guess)
  69. if(is.na(guess)){
  70. print("Wrong type. Please provide a number")
  71. } else {
  72. paste0("My number: ",number,". My guess: ",guess)
  73. if(guess==number) {
  74. print("You won. Gz")
  75. }else{
  76. print("loser.")
  77. }
  78. }
  79.  
  80. ##5.)
  81. myFunc<- function(withNA=TRUE,...){
  82.  
  83. args<-list(...)
  84. numberInput<-sapply(args,simplify2array)
  85. #a)
  86. paste("Number of arguments: ", length(args))
  87. #b)
  88. paste("Min: ",min(numberInput,na.rm = withNA)," Max: ",max(numberInput,na.rm = withNA))
  89. #c)
  90. paste("Sum of arugments:", sum(numberInput,na.rm = withNA))
  91. #d)
  92. paste("Mean of arguemnts: ", mean(numberInput,na.rm = withNA))
  93. }
  94.  
  95.  
  96. myFunc(withNA=TRUE,1,2,3,4,5)
  97.  
  98. ##7.)
  99. target_temp_1 <- c(21.9485903842751, 22.0784102698089, 21.9961920387344, 22.0888801363402,
  100. 21.7593642619783, 22.0733746321091, 20.0650945334316, 22.2239271878111,
  101. 22.0009439997692, 21.9857914565644)
  102. target_temp_2 <- c(-4.86905373694365, -4.80753142788851, -5.01604866437737, -5.04518643042135,
  103. -4.7522254125673, -5.14586954359958, -6.94227853304683, -5.00661640454364,
  104. -4.99321895338312, -5.32219568456159)
  105. target_temp_3 <- c(9.96164369257947, 10.2791653162905, 10.0121926692844, 10.0902680418902,
  106. 10.082372143524, 10.2616048271993, 7.97374718604113, 9.70072495469742,
  107. 9.99002115411479, 9.72302091276975)
  108. target_temp_4 <- c(39.1663079568986, 39.1015313287077, 39.0140282858949, 38.9695153382043,
  109. 39.1154016526277, 38.997730939927, 37.0125229951796, 38.8548037473053,
  110. 38.9950516925261, 38.9622025227075)
  111.  
  112. #a)
  113. temp1<-mean(target_temp_1)
  114. temp1
  115.  
  116. temp2<-mean(target_temp_2)
  117. temp2
  118. temp3<-mean(target_temp_3)
  119. temp3
  120. temp4<-mean(target_temp_4)
  121. temp4
  122. #b)
  123.  
  124. absoluteDeviation<-function(x){
  125. return(x-temp1)
  126. }
  127.  
  128.  
  129. lapply(target_temp_1,absoluteDeviation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement