Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

1. Return every nth element given a vector. O Project: (Nane) - - Source - L Run + = .O . @ Cato filc function

1. Return every nth element given a vector.

image text in transcribedimage text in transcribedimage text in transcribed

O Project: (Nane) - - Source - L Run + = .O . @ Cato filc function Addins - starter.r * o data-slicing (6). basic-functions.Rx H Source on 5ave 27 . 4 # This is the function skeleton for exercise 1: return every nth element given a vector 5 # Example usage from the R console: 6 # > V every.nth(v. 2) # should return the vector 4, 8, 12] since these occur at every second position 8 # every.nth(v. 3) should relurn the vector (6, 12] since these occur at every third position 9 V V mth. sum(v. 2) # should return 24 (= 4 - 8 + 12) nth. sur tapply v. categories, Sun) 0 1 2 3 2 10 18 12 Error: unexpected '}' in "}" > V -C(2,1,6,8,10,12) > n - 2 > nth.sum - Seq(2, length(v), by=n) > categories - findInterval(1: Length(), nth. sum) > tapplyv, categories, sum 0 1 2 3 2 10 18 12 O Project: (Nane) - .O . @ Gato filc function . Addins - starter.R' data-slicing (6.RX 0 basic-functions.Rx - H Source on Save 9 7 . 28 # Fill in logic here 29 -1 # Change this to the correct output if you attempt this problem. - Source - Run + = 32 | This is the function skeleton for exercise 3: 33 | For the given filename, read in the file into a data frame and return a new data frame containing every nth row of the original, 34 - every.nth row - function(filename, n) { 35 Fill in Tonic here 36 -1 # Change this to the correct output if you attempt this problem 39 # This is the function skeleton for exercise 4: get a vector containing all duplicated elements in the original vector, 48 Example usage from the R console: 41 > V duplicated. elements() # should return the vector [1, 4] since these occur more than once in v 43 - duplicated elements - function(vec) { 11 Fill in logic here 45 -1 * Change this to the correct output if you attempt this problem. 46 } 47 48 #This is the function skeleton for exercise 5: Compute the sums of consecutive groups of n clements in a vector 49 # Example usage from the R console: 50 # v- CC3,4,5,6,7,8,9,10) 51# consec. sums(v, 2) #Should return vector 7, 11, 15, 19) since 3-4-7. 5-6-11, 7-8-15, 9-10-19 (sums of groupings of 2 consecutive elements) 52 # > consec.sums(v, 3) # Should return vector [12, 21, 19] since 3+4-5-12, 6-7+8=21, 9+10=19 (sums of groupings of 3 consecutive elements - note the 53 Last group only has 2 since that is all that is left) 54 - consec. sums tapply v. categories, Sun) 0 1 2 3 2 10 18 12 Error: unexpected '}' in "}" > V -C(2,1,6,8,10,12) > n - 2 > nth.sum - Seq(2, length(v), by=n) > categories - findInterval(1: Length(), nth. sum) > tapplyv, categories, sum 0 1 2 3 2 10 18 12 DSCI 352 - Assignment 1 1) Crente an R function called every.nth which will take in the following as inparts: 1) a vector and 2) a number w. The function will return a new vector consisting of every nth element. 2) Create an R function called nth. sum which will take in the following as inputs: 1) a vector and 2) a number . The function will return the sum of every nth element in the vector. (I lint: Lise everywn from the previous exercise as a building block) 3) Crente nn R function called every.nth row which will take in the following as inputs: l) a filename to a data set and 2) a number. The fonction will read in the file intor data frame and return a new data frame condamng every olla row. (Hint: Use every loan one of the previous exercises as a building block). 4) Create an R function called duplicated. elements which will take in a veckr as ils only input. Thic function will return a new vector consisting of all clcunents which our MORE THAN ONCE in the input vector, (Hint consider using the built-in function able) 5) (CHALLENGE PROBLEM - Wurth 50 points) Create an R function called consec. sums which, given a vector of integers and a number as inputs, will generate the sum of consecutive groups of members and return the resulting sume in a new vector. If the initial vector's length is not an exact multiple af n, use as many as remain in computing the last sum. (*Yate: You may use the built-in sum function as a building block if you like) O Project: (Nane) - - Source - L Run + = .O . @ Cato filc function Addins - starter.r * o data-slicing (6). basic-functions.Rx H Source on 5ave 27 . 4 # This is the function skeleton for exercise 1: return every nth element given a vector 5 # Example usage from the R console: 6 # > V every.nth(v. 2) # should return the vector 4, 8, 12] since these occur at every second position 8 # every.nth(v. 3) should relurn the vector (6, 12] since these occur at every third position 9 V V mth. sum(v. 2) # should return 24 (= 4 - 8 + 12) nth. sur tapply v. categories, Sun) 0 1 2 3 2 10 18 12 Error: unexpected '}' in "}" > V -C(2,1,6,8,10,12) > n - 2 > nth.sum - Seq(2, length(v), by=n) > categories - findInterval(1: Length(), nth. sum) > tapplyv, categories, sum 0 1 2 3 2 10 18 12 O Project: (Nane) - .O . @ Gato filc function . Addins - starter.R' data-slicing (6.RX 0 basic-functions.Rx - H Source on Save 9 7 . 28 # Fill in logic here 29 -1 # Change this to the correct output if you attempt this problem. - Source - Run + = 32 | This is the function skeleton for exercise 3: 33 | For the given filename, read in the file into a data frame and return a new data frame containing every nth row of the original, 34 - every.nth row - function(filename, n) { 35 Fill in Tonic here 36 -1 # Change this to the correct output if you attempt this problem 39 # This is the function skeleton for exercise 4: get a vector containing all duplicated elements in the original vector, 48 Example usage from the R console: 41 > V duplicated. elements() # should return the vector [1, 4] since these occur more than once in v 43 - duplicated elements - function(vec) { 11 Fill in logic here 45 -1 * Change this to the correct output if you attempt this problem. 46 } 47 48 #This is the function skeleton for exercise 5: Compute the sums of consecutive groups of n clements in a vector 49 # Example usage from the R console: 50 # v- CC3,4,5,6,7,8,9,10) 51# consec. sums(v, 2) #Should return vector 7, 11, 15, 19) since 3-4-7. 5-6-11, 7-8-15, 9-10-19 (sums of groupings of 2 consecutive elements) 52 # > consec.sums(v, 3) # Should return vector [12, 21, 19] since 3+4-5-12, 6-7+8=21, 9+10=19 (sums of groupings of 3 consecutive elements - note the 53 Last group only has 2 since that is all that is left) 54 - consec. sums tapply v. categories, Sun) 0 1 2 3 2 10 18 12 Error: unexpected '}' in "}" > V -C(2,1,6,8,10,12) > n - 2 > nth.sum - Seq(2, length(v), by=n) > categories - findInterval(1: Length(), nth. sum) > tapplyv, categories, sum 0 1 2 3 2 10 18 12 DSCI 352 - Assignment 1 1) Crente an R function called every.nth which will take in the following as inparts: 1) a vector and 2) a number w. The function will return a new vector consisting of every nth element. 2) Create an R function called nth. sum which will take in the following as inputs: 1) a vector and 2) a number . The function will return the sum of every nth element in the vector. (I lint: Lise everywn from the previous exercise as a building block) 3) Crente nn R function called every.nth row which will take in the following as inputs: l) a filename to a data set and 2) a number. The fonction will read in the file intor data frame and return a new data frame condamng every olla row. (Hint: Use every loan one of the previous exercises as a building block). 4) Create an R function called duplicated. elements which will take in a veckr as ils only input. Thic function will return a new vector consisting of all clcunents which our MORE THAN ONCE in the input vector, (Hint consider using the built-in function able) 5) (CHALLENGE PROBLEM - Wurth 50 points) Create an R function called consec. sums which, given a vector of integers and a number as inputs, will generate the sum of consecutive groups of members and return the resulting sume in a new vector. If the initial vector's length is not an exact multiple af n, use as many as remain in computing the last sum. (*Yate: You may use the built-in sum function as a building block if you like)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions