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 03:38]
johannabradie
rworkshop8 [2014/11/03 16:18] (current)
johannabradie
Line 62: Line 62:
 </​code>​ </​code>​
  
-Curly brackets { } are used so that R knows to expect more input. ​ When using brackets, R waits to evaluate the command until the brackets have been closed. +Curly brackets { } are used so that R knows to expect more input. ​ When using brackets, R waits to evaluate the command until the brackets have been closed. ​ If the curly brackets are not used, R may not behave as you are expecting. ​ For example, try:
- +
-If the curly brackets are not used, R may not behave as you are expecting. ​ For example, try:+
  
 <code rsplus> <code rsplus>
Line 82: Line 80:
 }  # Since all brackets are now closed, R will evaluate the commands. }  # Since all brackets are now closed, R will evaluate the commands.
 </​code>​ </​code>​
 +
 +Note that if and if/else test a single condition. ​ If you want to test a vector of conditions (and get a vector of results), you can use ifelse:
 +
 +For example,
 +<code rsplus>
 +a<-1:10
 +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>​
 +
 +**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 ====
Line 147: Line 208:
 } }
 </​file>​ </​file>​
- 
  
 The expression part of the loop can be almost anything and is usually a compound statement containing many commands. The expression part of the loop can be almost anything and is usually a compound statement containing many commands.
Line 162: Line 222:
  
 //while loops// and //repeat loops// operate similarly to //for loops//​. ​ Once you understand how //for loops// work, you should be able to use any type of loop.  You will see some examples of //while loops// and //repeat loops// in the next section. //while loops// and //repeat loops// operate similarly to //for loops//​. ​ Once you understand how //for loops// work, you should be able to use any type of loop.  You will see some examples of //while loops// and //repeat loops// in the next section.
 +
 +**Exercise**
 +
 +NEED TO WRITE IN AN EXERCISE ​
  
 ==== Loop Modifications ==== ==== Loop Modifications ====
Line 170: Line 234:
  
 <file rsplus> <file rsplus>
 +
 +# Print the CO2 concentrations for "​chilled"​ treatments and keep count of how many replications there were.
 +
 +count=0
 for (i in 1:​length(CO2[,​1])) { for (i in 1:​length(CO2[,​1])) {
-  +  ​if (CO2$Treatment[i]=="​nonchilled"​) next #Skip to next iteration if treatment is nonchilled 
-repeat { +  ​count=count+1 
-    i <- i + 1 +  ​print(CO2$conc[i])
-    ​if (i %% 2 == 1) next  skip this loop +
-    print(i) +
-    if (i == 10) break     # stop looping+
 } }
-</​file>​+print(count) # The count and print command were performed 42 times.
  
 +# This could be equivalently written using a repeat loop: 
  
-<file rsplus> +count=0 
-<- 0+i=0
 repeat { repeat {
-    ​i <- i + 1 +      ​i <- i + 1 
-    if (i %% 2 == 1) next  # skip this loop +      if (CO2$Treatment[i]=="​nonchilled"​) next  # skip this loop 
-    print(i) +      ​count=count+1 
-    if (i == 10) break     # stop looping +      ​print(CO2$conc[i]
-} +      if (i == length(CO2[,​1])) break     # stop looping 
-</​file>​+     
  
-<file rsplus>​ +print(count)  
-i <- 0 + 
-repeat { +### This could also be written using a while loop: 
-  i <- i + 1 +
-  if (i %% 2 == 1) next +
-  ​print(i+
-  if (i == 10) break +
-+
-</​file>​ +
-------------------------------------------------------------------- +
-9: The equivalent ​while loop:+
  
 i <- 0 i <- 0
-while (i <= 10)+count=0 
 +while (i < length(CO2[,​1]))
 { {
   i <- i + 1   i <- i + 1
-  if (i %% 2 == 0) print(i)+  if (CO2$Treatment[i]=="​nonchilled"​next  # skip this loop 
 +  count=count+1 
 +  ​print(CO2$conc[i])
 } }
 +print(count) ​
 +</​file>​
  
-# ------------------------------------------------------------------- +**Exercise**
-# 10: And the equivalent for loop:+
  
-for (i in 1:10) +NEED TO WRITE IN AN EXERCISE ​
-  if (i %% 2 == 0) print (i)+
  
-# ------------------------------------------------------------------- +**Using flow control to make a complex plot**
-# 11: Or even more briefly, using seq() (read more in ?seq):+
  
-for (i in seq(210by=2)) print(i)+The idea here is that we have a dataset we want to plotwith concentration and uptake valuesbut each point has a type (Quebec or Mississippiand a treatment ​("​chilled"​ or "​nonchilled"​and we want to plot the points differently for these cases.  ​
  
 +You can read more about mathematical typesetting with ?plotmath, and more about the way # that different colors, sizes, rotations, etc. are used in ?par.
  
 +<file rsplus>
  
 +head(CO2) # Look at the dataset
 +unique(CO2$Type) ​
 +unique(CO2$Treatment)
 +
 +# plot the dataset, showing each type and treatment as a different colour
 +
 +plot(x=CO2$conc,​ y=CO2$uptake,​ type="​n",​ cex.lab=1.4,​xlab="​CO2 concentration",​ ylab="​CO2 uptake"​) # Type "​n"​ tells R to not actually plot the points.
 +     
 +for (i in 1:​length(CO2[,​1]))
 +{
 +  if (CO2$Type[i]=="​Quebec"&​CO2$Treatment[i]=="​nonchilled"​) {
 +    points(CO2$conc[i],​CO2$uptake[i],​col="​red",​type="​p"​) }
 +  if (CO2$Type[i]=="​Quebec"&​CO2$Treatment[i]=="​chilled"​) {
 +    points(CO2$conc[i],​CO2$uptake[i],​col="​blue"​) }
 +  if (CO2$Type[i]=="​Mississippi"&​CO2$Treatment[i]=="​nonchilled"​) {
 +    points(CO2$conc[i],​CO2$uptake[i],​col="​orange"​) }
 +  if (CO2$Type[i]=="​Mississippi"&​CO2$Treatment[i]=="​chilled"​) {
 +    points(CO2$conc[i],​CO2$uptake[i],​col="​green"​) }
 +}
 +
 +</​file>​
  
 +**Exercise**
  
 +NEED TO WRITE IN AN EXERCISE ​
  
 <file rsplus> <file rsplus>
 </​file>​ </​file>​