R coding assignment with working code
################################################################################ # MATH 2032 - Continuous Assessment Test 1 # SOLUTIONS ################################################################################ # This is a graded test worth 10% of your final mark. # Complete tasks listed below. Type your code in each section. # Provide discussions where required. # Save this file with your code and submit it through LearnOnline # Task 1 - (15 points) --------------------------------------------------------- # Create a variable "mydata" - a data structure with the output presented # below as comments # > print(mydata) # [[1]] # [1] "Some long text" # # [[2]] # [1] 1 2 3 4 5 # # $mix # $mix$a # [1] "text" # # $mix$b # a b c # 1 a -2 3 # 2 b 0 4 # 3 c 2 -5 # 4 d 4 77 # ---- your code here ---- # mydata <- data.frame(a="letters[1:4]," b="c(-2,0,2,4)" ,="" c="c(3,4,-5,77))" #="" use="" different="" ways="" of="" indexing="" (single="" and="" double="" squared="" brackets,="" names,="" #="" indexes,="" $-notation)="" to="" extract="" value="" 77="" from="" that="" object.="" #="" provide="" at="" lease="" three="" different="" options.="" #="" ----="" your="" code="" here="" ----="" #="" 1-="" mydata[c(4),"c"]="" [1]="" 77="" 2-="" mydata$c[c(4)]="" [1]="" 77="" 3-="" mydata[[4,3]]="" [1]="" 77="" #="" task="" 2="" -="" (20="" points)="" ---------------------------------------------------------="" #="" create="" a="" variable="" "df"="" -="" a="" data="" structure="" with="" the="" output="" presented="" #="" below="" as="" comments="" #=""> print(df) # x y z # 1 a -4 3 # 2 b -2 4 # 3 c 0 -5 # 4 d 2 6 # 5 e 4 -8 # ---- your code here ---- # df <- data.frame(x="letters[1:5]," y="c(-4,-2,0,2,4)" ,="" z="c(3,4,-5,6,-8))"> print(df) x y z 1 a -4 3 2 b -2 4 3 c 0 -5 4 d 2 6 5 e 4 -8 # Your main task is to change some values in "df": all negative numbers # should be multiplied by 10. Everything else remains the same. # Create two functions. Each function should take variable "df" as input and # output the same type of object but with changed values as per the rule above. # First function can use control flow operations - loops (for/while) and # selection/comparison (if/else). df <-c(-4,-2,0,2,4,3,4,-5,6,-8) res="">-c(-4,-2,0,2,4,3,4,-5,6,-8)><- c(length(10))="" for="" (i="" in="" df)="" {="" res="">-><- c(res="" ,="" i="" *="" 10)="" }="" res="" #="" second="" function="" can="" use="" only="" vectorisation/indexing="" -="" no="" loops.="" #="" ----="" your="" code="" here="" ----="" #="" #="" task="" 3="" -="" (15="" points)="" ---------------------------------------------------------="" #="" there="" are="" two="" very="" useful="" functions="" -="" max()="" and="" min().="" they="" return="" maximal="" #="" and="" minimal="" values="" of="" the="" numerical="" vector.="" #="" create="" your="" own="" custom="" function="" minmax()="" that="" does="" the="" same="" job.="" that="" #="" is,="" it="" takes="" a="" numerical="" vector="" as="" an="" input="" and="" outputs="" a="" vector="" with="" minimal="" #="" maximal="" of="" this="" vector="" values.="" #="" use="" only="" control="" flow="" operators="" -="" min/max="" functions.="" #="" prepare="" data="" and="" check="" min/max="" values="" test.vector="">-><- rnorm(10)="" test.vector="" #="" these="" are="" results="" you="" should="" get="" from="" your="" function="" c(min="min(test.vector)," max="max(test.vector))" #="" ----="" your="" code="" here="" ----="" #="" #="" task="" 4="" -="" (15="" points)="" ---------------------------------------------------------="" #="" take="" a="" vector="" below.="" assume="" that="" you="" start="" summation="" from="" the="" first="" element="" #="" adding="" element="" by="" element.="" your="" task="" is="" to="" find="" the="" maximal="" number="" #="" of="" elements="" you="" can="" take="" while="" the="" total="" summation="" stays="" below="" a="" given="" #="" threshold.="" #="" for="" example:="" x="">-><- c(1,="" 2,="" 3,="" 4,="" 5,="" 6).="" this="" summation="" (or="" running="" total,="" or="" #="" cumulative="" sum)="" is="" c(1,="" 3,="" 6,10,15,21).="" hence,="" for="" the="" summation="" to="" stay="" below="" #="" a="" threshold="" 13="" you="" can="" take="" 4="" first="" numbers.="" x="">-><- runif(50)="" #="" your="" test="" vector="" y="">-><- 10="" #="" your="" threshold="" #="" you="" have="" to="" make="" two="" custom="" functions,="" each="" function="" should="" take="" a="" vector="" #="" of="" numbers="" and="" threshold="" level;="" and="" it="" should="" return="" the="" number="" of="" values="" #="" to="" take="" before="" you="" hit="" a="" threshold.="" #="" first="" function="" should="" use="" control="" flow="" operators="" and="" no="" vectorisation.="" #="" second="" function="" should="" use="" vectorisation/indexing="" and="" function="" cumsum(),="" but="" #="" no="" control="" flow="" operators.="" #="" ----="" your="" code="" here="" ----="" #="" #="" task="" 5="" -="" (20="" points)="" ---------------------------------------------------------="" #="" load="" dataset="" "titanic"="" -="" count="" of="" passengers="" survived/died="" on="" titanic.="" #="" run="" the="" following="" code:="" data(titanic)="" print(titanic)="" titanic="" #="" check="" the="" help="" file="" for="" this="" data="" set="" #="" extract="" information="" and="" answer="" the="" following="" questions:="" #="" ----="" your="" code="" here="" ----="" #="" #="" 1.="" how="" many="" female="" crew="" members="" survived?="" #="" 2.="" how="" many="" people="" were="" on="" titanic="" in="" total?="" #="" 3.="" what="" proportion="" of="" females="" survived?="" #="" 4.="" what="" proportion="" of="" males="" survived?="" #="" 5.="" first-class="" passengers="" were="" on="" the="" higher="" decks="" and,="" as="" a="" result,="" they="" had="" #="" a="" higher="" chance="" to="" survive.="" how="" much="" higher="" was="" the="" probability="" to="" survive="" #="" for="" first-class="" passengers="" compared="" to="" the="" third="" class="" on="" the="" lower="" decks?="" #="" task="" 6="" -="" (15="" points)="" ---------------------------------------------------------="" #="" in="" this="" task="" you="" will="" do="" a="" text="" analysis.="" run="" the="" code="" below.="" #="" take="" a="" vector="" of="" characters="" below="" -="" variable="" "text".="" text="">-><- c("take="" a="" vector="" below",="" "assume="" that="" you="" start="" summation="" from="" the="" first="" element="" adding="" element="" by="" element",="" "your="" task="" is="" to="" find="" the="" maximal="" number="" of="" elements="" you="" can="" take="" while="" the="" total="" summation="" stays="" below="" some="" given="" threshold")="" #="" split="" the="" text="" in="" individual="" words.="" #="" you="" don't="" know="" this="" function="" -="" it="" is="" ok,="" you="" will="" learn="" it="" later="" (in="" week="" 9)="" #="" check="" the="" data="" structure="" of="" the="" variable="" "words"="" words="">-><- strsplit(text, split = " ") # create an array with counts of how many times a word with one, two, # three, ... etc. letters appears in each sentences. that is, the array with # 3 columns (one column per sentence) and 9 rows (1 letter, 2 letters, etc.) # ---- your code here ---- # # the end - don't forget to save your r-script --------------------------------- strsplit(text,="" split=" " )="" #="" create="" an="" array="" with="" counts="" of="" how="" many="" times="" a="" word="" with="" one,="" two,="" #="" three,="" ...="" etc.="" letters="" appears="" in="" each="" sentences.="" that="" is,="" the="" array="" with="" #="" 3="" columns="" (one="" column="" per="" sentence)="" and="" 9="" rows="" (1="" letter,="" 2="" letters,="" etc.)="" #="" ----="" your="" code="" here="" ----="" #="" #="" the="" end="" -="" don't="" forget="" to="" save="" your="" r-script="">- strsplit(text, split = " ") # create an array with counts of how many times a word with one, two, # three, ... etc. letters appears in each sentences. that is, the array with # 3 columns (one column per sentence) and 9 rows (1 letter, 2 letters, etc.) # ---- your code here ---- # # the end - don't forget to save your r-script --------------------------------->->->