Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1a. Create an R function called every.nth which will take in the following as inputs: 1) a vector and 2) a number n. The function

1a. Create an R function called every.nth which will take in the following as inputs: 1) a vector and 2) a number n. The function will return a new vector consisting of every nth element.

1b. Create an R function called nth.sum which will take in the following as inputs: 1) a vector and 2) a number n. The function will return the sum of every nth element in the vector. (Hint: Use every.nth from the previous exercise as a building block).

1c. Create an R function called every.nth.row which will take in the following as inputs: 1) a filename to a data set, and 2) a number n. The function will read in the file into a data frame and return a new data frame containing every nth row. (Hint: Use every.nth from one of the previous exercises as a building block).

1d. Create an R function called duplicated.elements which will take in a vector as its only input. The function will return a new vector consisting of all elements which occur MORE THAN ONCE in the input vector. (Hint: consider using the built-in function table).

1e. (CHALLENGE PROBLEM worth 50 points) Create an R function called consec.sums which, given a vector of integers and a number n as inputs, will generate the sums of consecutive groups of n numbers and return the resulting sums in a new vector. If the initial vectors length is not an exact multiple of n, use as many as remain in computing the last sum. (**Note: You may use the built-in sum function as a building block if you like)

image text in transcribedimage text in transcribed

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 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Development Of Knowledge Framework For Affective Content Analysis

Authors: Swarnangini Sinha

1st Edition

B0CQJ13WZ1, 979-8223977490

More Books

Students also viewed these Databases questions

Question

1. Does your voice project confidence? Authority?

Answered: 1 week ago