JustCaused

IS - Skripta 2

Jun 2nd, 2023 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. ###############################
  2. # Data sampling
  3. ###############################
  4.  
  5. # create a vector with values from 1 to 10
  6.  
  7. # create a sample of size 5 from the vector
  8.  
  9. # create a sample of size 20 from the vector, where duplicates are allowed
  10.  
  11. # set seed and create two sample of size 20 from the vector, where duplicates are allowed
  12.  
  13. ###############################
  14. # Matrices
  15. ###############################
  16.  
  17. # create a 2 x 4 matrix with values from 8 to 1, filled by rows
  18.  
  19. # get the first row
  20.  
  21. # get the element from row 1, column 2
  22.  
  23.  
  24. # get number of rows
  25.  
  26. # get number of columns
  27.  
  28. # create two matrices of the same dimension
  29.  
  30. # add matrix2 to matrix1
  31. # transpose a matrix
  32.  
  33. ###############################
  34. # Lists
  35. ###############################
  36.  
  37. # create a new list with attributes: passport, age, diplomatic
  38.  
  39. # get the 2nd element
  40.  
  41. # get the value of the 2nd element
  42.  
  43. # get the value of the age element
  44.  
  45. # get the list length
  46.  
  47. # add new list after the 2nd element
  48.  
  49. # delete 3rd element
  50.  
  51. # concatinate two lists
  52.  
  53. # check if travelers is a list
  54.  
  55. # get names of all list elements
  56.  
  57. # get elements with 'age' in their name
  58.  
  59. ###############################
  60. # Foreach loop
  61. ###############################
  62.  
  63. # print all odd numbers from 1 to 10 using for each loop
  64.  
  65. ###############################
  66. # While loop
  67. ###############################
  68.  
  69. # print all odd numbers from 1 to 10 using while loop
  70.  
  71. ###############################
  72. # Task 1
  73. ###############################
  74. #Create a 2 x 3 matrix with the following elements: 3, 9, -1, 4, 2, 6 (by row). Print only the positive values from the first row.
  75.  
  76. # Answer:
  77. matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
  78.  
  79. for (i in matrix1[1,]) {
  80. if (i > 0) {
  81. print(i)
  82. }
  83. }
  84.  
  85. ###############################
  86. ## if-else
  87. ###############################
  88.  
  89. # use ifelse function to create a new attribute called 'request' with the value 'assistance required' if a traveler is younger than 10 years, and the value 'no special requests' otherwise
  90.  
  91. ########################################
  92. # User-defined functions and apply
  93. ########################################
  94.  
  95. # create a function that adds two numbers. The default value for the second argument is 1
  96.  
  97. # create a function returning an absolute value of x. Return the result using the return() function
  98.  
  99. ##############################################################
  100. # Applying a function over rows and columns in data frame
  101. ##############################################################
  102.  
  103. # load the data "data/beatles_v2.csv"
  104.  
  105. # get the number of characters in the song title "Yellow Submarine"
  106.  
  107.  
  108. # get the number of characters of the first 10 songs
  109.  
  110.  
  111. # calculate the mean value of the duration and Top.50.Billboard values of all songs from 1963
  112.  
  113.  
  114. # calculate the mean value of the duration and Top.50.Billboard values that are not NAs of all songs from 1963
  115.  
  116.  
  117. ###############################
  118. # Working with tables
  119. ###############################
  120.  
  121. # create a contingency table of column Year values
  122.  
  123. # get the 4th element from the table
  124.  
  125. # store the 4th element from the table in a variable
  126.  
  127. # convert the variable to numeric
  128.  
  129. # sort the table in the descending order
  130.  
  131. # get the proportions table for the values of the Year column
  132.  
  133. # sort the proportions table in the descending order
  134.  
  135. # get the proportions table for the values of the Year column, but limiting number of digits to 2
  136.  
  137. # create a contingency table Top.50.Billboard vs. Year
  138.  
  139. ###############################
  140. # Manipulating data frames
  141. ###############################
  142.  
  143. ###############################
  144. ## Adding new rows and columns
  145. ###############################
  146.  
  147. # create a new column On.album and set FALSE for all songs
  148.  
  149. # create a new data frame with two columns (with sample data)
  150.  
  151. # combine two data frames
  152.  
  153. # get the first song
  154.  
  155. # add the song to the end of the data frame
  156.  
  157. # add the song after the 3rd song in the data frame
  158.  
  159. ###############################
  160. ## Removing columns and rows
  161. ###############################
  162.  
  163. # remove the attribute On.album
  164.  
  165. # remove columns Platinum (at index 10) and Score (at index 11)
  166.  
  167. # create a subset of the data frame without songs in rows 2, 4 and 6
  168.  
  169. # create a subset of the data frame without songs in rows from 1 to 8
  170.  
  171. ##################################
  172. ## Updating column and row names
  173. #################################
  174.  
  175. # get column names
  176.  
  177. # change name of the column that starts with 'Genre' to 'Song.genre'
  178.  
  179. # change name of the column at the index 6 to 'Genre'
  180.  
  181. # change row names to a string containing word 'song' and a song order number
  182.  
  183. # change row names to a string containing order number
  184.  
  185. ##################################
  186. ## Retrieving and changing values
  187. ##################################
  188.  
  189. # get songs in rows from 1 to 5, but only attributes Title and Album.debut
  190.  
  191. # get the songs from year 1964 not having McCartney as a lead vocal
  192.  
  193. # get the songs from year 1958, but only attributes Title and Album.debut
  194.  
  195. # create a vector of logical values denoting whether the attribute Album.debut has a value or not
  196.  
  197. # compute how many songs lack the data about the debut album
  198.  
  199. # for songs without debut album data, set the value of the Album.debut attribute to 'empty'
  200.  
  201. # set the value back to empty string
  202.  
  203. ###############################
  204. ## Saving dataset
  205. ###############################
  206.  
  207. # save dataset to a CSV file, but without the row names (row numbers) column
  208.  
  209. # save R object for the next session into file "data/beatles_v3.RData"
  210.  
  211. # restore R object from the file "data/beatles_v3.RData" in the next session
  212.  
  213. ###############################
  214. # Task 2
  215. ###############################
  216.  
  217. # Create a new column in the *beatles* data frame called *Billboard.hit* having TRUE for all songs that were in the Top 50 Billboard (songs that have the Top.50.Billboard defined), and FALSE for all other songs (not having this value set).
  218.  
  219. # Answer:
  220.  
Advertisement
Add Comment
Please, Sign In to add comment