I want to write a function that does the following. It has two arguments, arg1 and arg2 and arg2 has the default value set to 1. When the function is called up users assign a data frame/matrix to arg 1 and assign to arg2 either a 1, 2, or 3.
if arg1 is assigned the value 1 the function returns the vector with all its column names
if arg 2 is assigned the value 2, the function returns the mean of every column
if arg2 is assigned the value 3 the function returns the medians of every row.
if arg2 is assigned anything outside 1, 2, or 3 the function prints an error message.
Here's my code so far:
my_Func1<-function(arg1)
A<-data.matrix(arg1)
if(arg2==1){
return(colnames(arg1))
}
else {arg2==2){
colMeans(A)
}
else{arg2==3){
rowMedians(A)
}
else if (missing(arg2))
writeLines(“ The value must be 1, 2, or 3”)
}
}
On my machine it compiles sometimes and produces empty matrices or it just simply does not compile. I don't quite know where the error is, but I suspect its in the syntax. Can someone help me determine what's wrong and advise on what changes I need to make? Or it might be my logic?