Second max value of a list. We will write a simple function that has a list of numbers as input, then output the value of the second largest (return -infinity if there is only one item in the list).
secMax<-function(x)
{
infty<-10^20; #define this as a very small number;
n<-length(x);
if(n <2) return(-infty);
mysec<--infty;
mymax<-x[1];
for(i in 2:n)
{
if(x[i] > mymax)
{
mysec<-mymax;
mymax<-x[i];
}
else
{
if(x[i] > mysec)
{
mysec<-x[i];
}
}
}
return(mysec);
}