Wednesday, June 28, 2017

order char in R

big2small = function(char1, char2) {
  if ( char1 > char2) {
    return( c( char1, char2) )
  } else {
    return( c(char2, char1) )
  }
}
name1 = c("apple",  "banana", "dog", "cat")
name2 = c("banana", "apple",  "cat", "dog")
tb = cbind(name1, name2)
for( i in 1:4){
  return= big2small(tb[i, 1], tb[i, 2])
  print(return)
}
##    name2    name1 
## "banana"  "apple" 
##    name1    name2 
## "banana"  "apple" 
## name1 name2 
## "dog" "cat" 
## name2 name1 
## "dog" "cat"

No comments:

Post a Comment