Drill 1: Using loops

Description

Create a for loop that outputs the arithmetic mean for each variable (column) of your data frame df – provided that the variable is numeric!

Don't use plagiarized sources. Get Your Custom Assignment on
Drill 1: Using loops
From as Little as $13/Page

Unformatted Attachment Preview

R Programming
Week 2.2: The R language – “Paragraphs” and “Essays”
Writing scripts
Control of execution flow
if statements
loops
iterations
Writing scripts
Script – a text file that contains commands that you would type at the console
prompt:
o The file contains valid R statements (including comments) and nothing else
o Comments start at a # and end at the end of the line
o The R statements are in the file in the order that they must be executed
o R scripts have file names ending in .r or .R
o script can be “sourced” using function source()
my.first.script.r file:
# this is my first R script
print(3 + 4)
source the file:
source(“my.first.script.r”)
## [1] 7
Writing scripts
By now you should be familiar enough with R to be able to write your own script:
1. Create a new script (from the File menu, “+” icon, or by typing “Ctrl + Shift + N”).
2. Save the file as “my.second.script.r”.
3. Use the editor pane in RStudio to type some R commands and comments.
4. Run individual commands.
5. Source the whole file.
How does one achieve an understandable script or program?
1. Avoid the unusual. As a minimum try to be consistent with yourself.
2. Use meaningful names for variables, and any other object.
3. Traditionally in R one uses dots to separate the words and use only lower case.
Writing scripts
Diagnosing the source of bugs is (like detective work):
o hunches based on common sense and experience
o follow different leads until the case is solved
o rely on some sort of divide-and-conquer strategy
Special tools, called debuggers:
o allow one to step through the code, executing one statement at a time
o R debugger is available within RStudio
o it is wise to very carefully read the documentation
Control of execution flow
Control of execution statements allows flexibility
Individual statements can be grouped into compound statements with curly braces
print(“A”)
## [1] “A”
{
print(“B”)
print(“C”)
}
## [1] “B”
## [1] “C”
if, else
flag
Purchase answer to see full
attachment