0
votes

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?

1
I don't get it. Please supply some example input calls and what you expect to see from the result of those calls - Rich Scriven

1 Answers

1
votes

The following seems to be working:

library(matrixStats) #for rowMedians
my_Func1<-function(arg1, arg2=1){

  A<-data.matrix(arg1)

  if(arg2==1){
    return(colnames(arg1))
  }
  else if (arg2==2){
    colMeans(A)
  }
  else if (arg2==3){
    rowMedians(A)
    #or use apply(A, 1, median) 
    #if you don't want to use the matrixStats package
    #result is the same
  }
  else{
    stop("The value must be 1, 2, or 3")
  }

}

As an example:

df <- data.frame(a=runif(100), b=runif(100))

Outputs:

#returns colnames
my_Func1(df)
[1] "a" "b"

#returns col means
my_Func1(df,2)
        a         b 
0.5115115 0.5179713 

#returns rowMedians
my_Func1(df,3)
 [1] 0.6095942 0.8047744 0.9322312 0.7202797 0.4061614 0.9268534 0.6453481 0.6332645 0.3356115 0.6845784 0.3562386 0.8149613 0.3368132
 [14] 0.3943005 0.4082845 0.6249351 0.7483373 0.7649562 0.5062454 0.4025369 0.4864001 0.4748679 0.5620104 0.9720910 0.3495394 0.8274248
 [27] 0.4315357 0.2935979 0.4365097 0.6043698 0.4077471 0.2718737 0.6230104 0.5795740 0.3220522 0.3377562 0.4806867 0.4550451 0.8740976
 [40] 0.7058154 0.4779405 0.5774614 0.6220152 0.4811471 0.3716731 0.4404327 0.6374183 0.4995577 0.2696681 0.5154605 0.1612755 0.5637899
 [53] 0.5148926 0.2683836 0.5989326 0.5168307 0.6084937 0.1851450 0.8010263 0.4063176 0.2227938 0.5751372 0.4599596 0.4134101 0.6519764
 [66] 0.7971018 0.3114643 0.2374720 0.8218233 0.9588351 0.7158696 0.6633277 0.4514450 0.2356745 0.5297823 0.4686703 0.3113826 0.2800651
 [79] 0.4709474 0.4831431 0.3746795 0.2379049 0.2086487 0.2870380 0.6961195 0.6039862 0.5702595 0.7620196 0.5670314 0.4874534 0.2331586
 [92] 0.4843724 0.6366822 0.5150960 0.4336061 0.2913745 0.4006255 0.4996965 0.6356846 0.4182479

#returns error
my_Func1(pval,4)
Error in my_Func1(df, 4) : The value must be 1, 2, or 3

In your code you are using else instead of else if and you also open left curly brackets { and close them with brackets ). Also, if you want an error if argument 2 is not 1 or 2 or 3 then you need function stop.