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
r_workshop5 [2018/11/14 16:28]
katherinehebert [for loop]
r_workshop5 [2019/08/08 17:55] (current)
mariehbrice [Workshop 5: Programming in R]
Line 13: Line 13:
 **Summary:​** This workshop focuses on basic programming in R.  In this workshop, you will learn how to use control flow (for loops, if, while) methods to prevent code repetition, facilitate organization and run simulations. ​ In addition, you will learn to write your own functions, and tips to program efficiently. The last part of the workshop will discuss packages that will not covered elsewhere in this workshop series, but that may be of interest to participants. **Summary:​** This workshop focuses on basic programming in R.  In this workshop, you will learn how to use control flow (for loops, if, while) methods to prevent code repetition, facilitate organization and run simulations. ​ In addition, you will learn to write your own functions, and tips to program efficiently. The last part of the workshop will discuss packages that will not covered elsewhere in this workshop series, but that may be of interest to participants.
  
-**Link to new [[https://​qcbsrworkshops.github.io/Workshops/​workshop05/​workshop05-en/​workshop05-en.html|Rmarkdown presentation]]**+**Link to new [[https://​qcbsrworkshops.github.io/​workshop05/​workshop05-en/​workshop05-en.html|Rmarkdown presentation]]**
  
 Link to old [[https://​prezi.com/​xuu2rphp5wg4/​|Prezi presentation]] Link to old [[https://​prezi.com/​xuu2rphp5wg4/​|Prezi presentation]]
Line 289: Line 289:
 </​code>​ </​code>​
  
-Tip1. To loop over the number of rows of a data frame, we can use the function ''​nrow()''​+**Tip 1.** To loop over the number of rows of a data frame, we can use the function ''​nrow()''​
  
 <code rsplus> <code rsplus>
Line 297: Line 297:
 </​code>​ </​code>​
  
-Tip2. If we want to perform operations on the elements of one column, we can directly iterate over it+**Tip 2.** If we want to perform operations on the elements of one column, we can directly iterate over it
  
 <code rsplus> <code rsplus>
Line 305: Line 305:
 </​code>​ </​code>​
  
-The expression within the loop can be almost anything and is usually a compound statement containing many commands.+**Tip 3.** The expression within the loop can be almost anything and is usually a compound statement containing many commands.
  
 <code rsplus> <code rsplus>
Line 917: Line 917:
 Proper indentation and spacing is the first step to get an easy to read code: Proper indentation and spacing is the first step to get an easy to read code:
  
-Use spaces between and after you operators; +  * Use spaces between and after you operators 
-Use consistentely ​the same assignation operator. <- is often preferred. = is OK, but do not switch all the time between the two; +  ​* ​Use consistently ​the same assignation operator. <- is often preferred. = is OK, but do not switch all the time between the two 
-Use brackets when using flow control statements:+  ​* ​Use brackets when using flow control statements: 
 +    * Inside brackets, indent by *at least* two spaces; 
 +    * Put closing brackets on a separate line, except when preceding an `else` statement.  
 +  * Define each variable on its own line.
  
-- Inside brackets, indent by *at least* two spaces; - Put closing brackets on a separate line, except when preceding an `else` statement.  +This code is not spaced, and therefore hard to read. All brackets are badly aligned, and it looks "​messy"​. ​
-- Define each variable on its own line. +
- +
-code is not spaced. All brackets are in the same line, and it looks "​messy"​. ​+
 <code rsplus> <code rsplus>
 a<-4;b=3 a<-4;b=3
Line 932: Line 932:
 </​code> ​ </​code> ​
  
-it looks more organized, no?+This looks more organized, no?
  
 <code rsplus> <code rsplus>