Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rworkshop8 [2014/11/03 04:20]
johannabradie
rworkshop8 [2014/11/03 16:18] (current)
johannabradie
Line 87: Line 87:
 a<-1:10 a<-1:10
 ifelse(a>​5,"​yes","​no"​) ifelse(a>​5,"​yes","​no"​)
 +</​code>​
 +
 +You can also use ifelse in a function to apply a function only under certain conditions:
 +
 +
 +For example,
 +<code rsplus>
 +a<​-(-4):​5
 +sqrt(ifelse(a>​=0,​a,​NA))
 </​code>​ </​code>​
  
 **Exercise** **Exercise**
  
-NEED TO WRITE IN AN EXERCISE ​+<code rsplus>​ 
 +Paws<​-"​cat"​ 
 +Scruffy<​-"​dog"​ 
 +Sassy<​-cat 
 +animals<​-c(Paws,​Scruffy,​Sassy) 
 +</​code>​ 
 + 
 +1. Use an if statement to print "​meow"​ if Paws is a "​cat"​. 
 + 
 +<​hidden>​ 
 +<code rsplus>​ 
 +if(Paws=="​cat"​) { 
 +  print("​meow"​)} 
 +</​code>​ 
 +</​hidden>​ 
 + 
 +2. Use an if/else statement to print "​woof"​ if you supply an object that is a "​dog"​ and "​meow"​ if it is not.  Try it out with Paws and Scruffy. 
 + 
 + 
 +<​hidden>​ 
 +<code rsplus>​ 
 +if(Scruffy=="​dog"​) { 
 +  print("​woof"​)  
 +} else { 
 +  print ("​meow"​) 
 +
 + 
 +if(Paws=="​dog"​) { 
 +  print("​woof"​)  
 +} else { 
 +  print ("​meow"​) 
 +
 +</​code>​ 
 +</​hidden>​ 
 + 
 +3. Use an ifelse statement to display "​woof"​ for animals that are dogs and "​meow"​ for animals that are cats. 
 + 
 +<​hidden>​ 
 +<code rsplus>​ 
 +ifelse(animals=="​dog","​woof","​meow"​) 
 +</​code>​ 
 +</​hidden>​ 
  
 ==== Loops ==== ==== Loops ====