Writing an R Functions for vectors
Statistics Homework in R Studio
- Write an R function called Cleaner that accepts a single vector of numbers that may contain NA entries and returns a vector where the NA’s have been replaced with -1.
- Write an R function that accepts three parameters: a lower bound, an upper bound, and an increment. Then use a repeat loop to generate a vector of the numbers from the lower bound to the upper bound by increment.
For example, if my function was called counter
answer <- counter(2,10,2)
answer
[1] 2 4 6 8 10
i.e. the numbers from 2 to 10 in increments of 2
answer <- counter(2,10,3)
answer
[1] 2 5 8 - Assuming I have three variables called lower, upper, and increment, how could I produce the same thing as number 2 with a single R statement that does not employ a loop?
- Write an R function that accepts two parameters: a vector of strings and a single search character. The function will then return a vector that contains the input strings that contain the search character.
For Example, if my function was called searcher
names <- c(“Bob”, “Bill”, “William”,”Tom”)
answer <- searcher(names,”i”)
answer
[1] “Bill” “William”
- Write an R function that accepts three parameters: a vector of strings, a single search character, and a single replacement character. The function will return the vector of strings, but with all instances of the search character replaced with the replacement character.
For example, if my function was called replacer
names <- c(“Bob”, “Bill”, “William”,”Tom”)
answer <- replacer(names, “o”, “O”)
answer
[1] “BOb” “Bill” “William” “TOm"