- Write a function that will take print out your name and call it myName
myName <- function(){
print("Brian Pattiz")
}
myName()
## [1] "Brian Pattiz"
- Write a function that will return the sum of two squares and call it SumofTwoSquares. Test it!
SumofTwoSquares <- function(x, y){
return(x^(2) + y^(2))
}
SumofTwoSquares(3, 3)
## [1] 18
- Write a function called ReturnNRowNCol that will take a dataframe and return both its number of rows and number of columns in a vector. Test it with the following dataframe:
admits <- data.frame(ADMIT_ID = c(paste0("000", 1:9),"0010"), ACT = round(runif(10, 17, 34)),
HIGH_SCHOOL_DESC = c("Blue Springs South", "Warrensburg High School", "Odessa R-VII SR High School", "Blue Springs South", "Lees Summit North High School", "Lees Summit West High School",
rep("Warrensburg High School", 3), "Platt County R-III High School"), stringsAsFactors = FALSE)
ReturnNRowNCol <- function(df){
numrow <- nrow(df)
numcol <- ncol(df)
return(c(numrow, numcol))
}
ReturnNRowNCol(admits)
## [1] 10 3
- Write a function that take any list and print if the last element is a character string. Call it isString. Test with x <- list(T, “This is a test”, 4L, 5L, F, T, “This is a string”)
isString <- function(test_list){
len <- length(test_list)
is.character(test_list[[len]])
}
x <- list(TRUE, "This is a test", 4L, 5L, F, T, "This is a string")
isString(x)
## [1] TRUE