In: Computer Science
1. You are given R function below:
TestFunction <- function(df){
M <- rep(0,ncol(df)) for(i in 1:ncol(df)){ M[i] <- max(df[,i],na.rm=TRUE) }
return(M) } colMax <- function(df){
M <- rep(0,ncol(df))
for(i in 1:ncol(df)){
M[i] <- max(df[,i])
}
return(M)
}
Describe briefly what this function does
Explanation is given in the columns
Function - 1 :
TestFunction <- function(df){ # this function computes the dataframe df
M <- rep(0,ncol(df)) # ncol(df) will return the number of columns in the dataframe
# rep functions fills the vector with 0 of size 11 and stores the vector in M
for(i in 1:ncol(df)){ M[i] <- max(df[,i],na.rm=TRUE) }
# returns the max element in every column. A problem can occur, when your data contains # NA values (i.e. missing data). //Here the option na.rm=TRUE within the max function
# says that the NULL values are present in the column and asif it is true ignores NA and
# returns max value. If the na.rm=TRUE is not true then and if the NULL values present in
# the column then max becomes NA. If na.rm=TRUE then the max would be the maximum # number.
return(M) }
Function - 2 :
colMax <- function(df){ # this function computes the dataframe df
M <- rep(0,ncol(df))# ncol(df) will return the number of columns in the dataframe
# rep functions fills the vector with 0 of size 11 and stores the vector in M
for(i in 1:ncol(df)){
M[i] <- max(df[,i])
# returns the max element in every column. A problem can occur, when your data contains # NA values (i.e. missing data). //Here the option na.rm=TRUE within the max function
# says that the NULL values are present in the column and asif it is true ignores NA and
# returns max value. If the na.rm=TRUE is not true then and if the NULL values present in
# the column then max becomes NA. If na.rm=TRUE then the max would be the maximum # number.
}
return(M)
}