Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Learning R Ecology 180.docx Welcome to Learning R! This workshop is designed for folks with a wide range of previous experience in R, from complete beginners to experienced R-coders. The course is...

1 answer below »
Learning R Ecology 180.docx
Welcome to Learning R!
This workshop is designed for folks with a wide range of previous experience in R, from complete
eginners to experienced R-coders. The course is organized around a series of assignments that you
work on at home, with weekly meetings to troubleshoot and discuss issues that arise.
Intro: What's Going On?
R is a powerful, flexible, and dynamic language for scientific programming, plotting and statistical
analysis. It is free and open-source and has a big community of users that are constantly developing new
"packages" that increase the capability of the language. R is the software engine that runs our code. In R,
code is written as a "script" - a series of commands that get fed to R one at a time.
R Studio is an integrated development environment (IDE) for R. It is free software that provides a nice
environment for developing code. R Studio makes your code look better and provides convenient tools
that make your code faster and easier to read and write.
R Markdown is a kind of R script that is especially well-suited for collaborative data analysis. It makes it
easier to write notes, integrate figures, and generate reports to share your analysis with others. You
normal R code is still readily available and accessible inside "code chunks".
The tidyverse is a set of R packages that provide an alternative way to do many things in R, from data
manipulation to plotting. When you install the tidyverse package, you are installing several component
packages; key among them is ggplot2 (a plotting package) and dplyr (a data manipulation package). In
comparison, "base R” is R using only the packages that come with a standard installation. We will use
oth.
General Advice: We Can Work It Out
1. If you are new to R, challenge yourself to type in new commands instead of cutting and pasting.
Typing forces you to learn the conventions of unfamiliar script, and to figure out which parts of
the syntax are flexible and which are not. Once you are more familar with the syntax, you can
start using copy/paste and other tricks to move faster.
2. Try taking apart long nested commands from the inside out. Break up the long command into
manageable parts. Try to understand how the computer sees each part.
3. Em
ace the struggle, but don’t wo
y to much if you get stuck.
4. Use the help (?) function often and learn how to read the documentation efficiently.
5. Use the internet. Even the pros use the internet to remember synatax, figure out the best
approach and learn new tools.
6. Aim to write consistent and organized code with clear headings and consistent naming
conventions. Provide enough expla.nation of your code so that others can understand what you
did.
7. Make sure your script can run cleanly from the top down, without internal conflicts.
8. Be creative and
ave. There are usually many ways to do the same thing in R. Look for ways that
are efficient, clear and elegant.
https:
www.r-project.org
https:
www.rstudio.com
https:
markdown.rstudio.com
https:
www.tidyverse.org
Step 1: People Get Ready
1. Install R and RStudio.
2. Open RStudio.
3. A
ange RStudio so that you can see the console, environment, and help windows.
4. Go to Tools>Global Options>Appearance and choose an "Editor theme" that you'll want to stare
at for several hours at a time.
5. Create a new R Markdown (Rmd) file. Title it something like “ Learning R”, add you
name as the author, and set the output to HTML. This will open in a new window - the script
window.
6. Create a folder with your name in the Google Drive “Learning R” folder. Save your Rmd file to
this folder.
7. Knit and view your Rmd report. Your report should be an HTML file. Compare the report with the
script.
8. Change the heading "R Markdown" to read "Step 1: People Get Ready" and re-knit. What
happens?
9. Delete everything below your heading.
10. Insert a new R chunk below your heading. The boundaries of an R chunk are just text. Try to
eak the chunk and fix it again.
11. Type data() into the console and run it to view a list of example datasets.
12. Type ?data into the console to view a description of the data function in the help window.
Inspect the help documentation closely; every function in R has its own help documentation in
the same format. Try to understand what each section of this help documentation does.
13. Type the name of the dataset pressure into the console.
14. Type pressure$temperature into the console. What does the "$" symbol do? What would
pressure$pressure return?
15. Type plot(pressure$temperature, pressure$pressure) into the console.
16. Type plot(pressure$temperature, pressure$pressure) into your new R chunk and run it. What is
the difference between the console and the script windows?
17. Type ?plot into the console to view a description of the plot function.
18. What would the command plot(pressure$pressure, pressure$temperature) return? Make a
prediction in your head before you run it in your chunk.
19. Plot some data from the datasets CO2 and chickwts the same way.
20. At this point, you should have several lines in your chunk. Figure out how to run one line at time
("Run Selected Line(s)"), how to run the entire cu
ent chunk ("Run Cu
ent Chunk"), and how to
un the entire script ("Run All").
21. Search for an R Markdown reference guide or cheatsheet online.
22. Write a short description of what you did/learned at the end of your Rmd file, after your chunk.
Be sure to explain what the script, console, and help windows show. Also write a sentence that
includes italics, bold, and a link to an interesting website about one of your ho
ies.
23. When you close RStudio, be sure to save any changes to your Rmd, but don’t save you
workspace.
Step 2: The Way You Do The Things You Do
1. Open your Rmd in RStudio.
2. Create a new heading ("Step 2: The Way You Do The Things You Do") and make a new R chunk.
3. Type write.csv(pressure,file="pressure.csv") into the new Rmd chunk, then run it.
4. Look for the file “pressure.csv” in your folder. Open it in a spreadsheet program like Excel, then
close it.
5. Type ?write.csv into the console. Read the description in your help window and try to
understand what how the write.csv function works.
6. Type read.csv("pressure.csv") into your Rmd chunk below the write.csv command, then run the
chunk again. What happened?
7. Edit your read.csv command to say d1<-read.csv("pressure.csv"), then run the chunk again. Look
in your environment window. What happened this time?
8. Click on the "d1" in the environment window.
9. Click on the small blue triangle to the left of the "d" in the *environment* window.
10. Type d1 into your chunk and run it.
11. Type plot(d1$pressure, d1$temperature, col="blue", pch=2, type="b", cex=0.5) into the chunk
and run it.
12. Read the help documentation for the base R plot function ("plot") and the graphical parameters
function ("par").
13. Search the internet for more information about graphical parameters and colors in R.
14. Plot a new figure using a built-in dataset from data(). Make it colorful.
15. Create a new, fake dataset in a CSV spreadsheet (or use a real one of your own) and import it
into R as "d2".
16. Plot these data any way you want to.
17. Any text preceded by a hashtag (“#”) inside of a chunk becomes a comment – a short
human-readable note that is not evaluated by R. Comments allow you to organize and explain
your code better. Add a few to your chunk.
18. Run your entire script from the top (Code > Run Region > Run All).
19. Write a short description of what you did/learned at the end of your Rmd file, after your chunk.
20. When you close RStudio, be sure to save any changes to your Rmd, but don’t save you
workspace.
Step 3: Starting All Over Again
1. Open your Rmd in RStudio.
2. Type install.packages("tidyverse") into the console and run it.
3. Type li
ary(tidyverse) into the setup chunk of your Rmd. The setup chunk is the one labeled “{
setup, include=FALSE}”
4. While you are at it, make a new chunk just below your setup chunk. Label it {r clear workspace},
and add the two lines rm(list=ls()) and graphics.off() to this chunk. These lines will clear the
workspace so that you have a clean slate whenever you run the entire script from the top.
5. Run your entire script from the top (Code > Run Region > Run All).
6. Create a new heading (“Step 3: Starting All Over Again”) at the bottom of your script and make a
new chunk.
7. Type ggplot(cars, aes(x=speed, y=dist)) +geom_point() +geom_smooth(formula=y~x,
method="lm") and run the chunk.
8. Type ggplot(chickwts, aes(x=feed, y=weight, fill=feed)) +geom_col() and run the chunk.
9. Type ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, col=Species, group=Species))
+geom_point() +geom_smooth(formula=y~x, method="lm") and run the chunk.
10. Type ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, col=Species, size=Petal.Width,
group=Species)) +geom_point(alpha=0.5) +geom_smooth(formula=y~x, method="lm")
+facet_wrap(~Species, nrow=1, scales="free") +theme(legend.position = "bottom") and run
the chunk.
11. Notice how I put spaces after commas and before the plus sign? It helps with text wrapping.
12. Go to https:
ggplot2.tidyverse.org
eference/index.html and figure out what you’ve done.
13. Add
ief comments to your chunk before each ggplot() call.
14. Create another plot using a different dataset. You can use any combination of base R and ggplot.
Try to make plots that are both information-rich and aesthetically compelling.
15. Write a short description of what you did/learned at the end of your Rmd file, after your chunk.
16. When you close RStudio, be sure to save any changes to your Rmd, but don’t save you
workspace.
Step 4: Can You Get To That
1. Open your Rmd in RStudio.
2. Run your entire script from the top (Code > Run Region > Run All).
3. Create a new heading (“Step 4: It’s Your Thing”) and make a new chunk.
4. Type a<-c(1,2,3,4) into the chunk and run it. (Note your environment window.)
5. Type a into the chunk and run it.
6. Type
-c(5,6,7,8) into the chunk and run it.
7. Type b into the chunk and run it.
8. Type a*b into the chunk and run it.
9. Type a+b into the chunk and run it.
10. Type b[3] into the chunk and run it.
11. Type b[1:3] into the chunk and run it.
12. Type a[3]+b[3] into the chunk and run it.
13. Type iris into the chunk and run it.
14. Type head(iris) into the chunk and run it.
15. Type tail(iris) into the chunk and run it.
16. Type names(iris) into the chunk and run it.
17. Type unique(iris$Species) into the chunk and run it.
18. Type iris[1,] into the chunk and run it.
19. Type iris[,1] into the chunk and run it.
20. Type iris[1:10,1] into the chunk and run it.
21. Type iris[1:10,"Petal.Length"] into the chunk and run it.
22. Type iris[,"Petal.Length"] into the chunk and run it.
23. Type iris[,"Sepal.Length"] into the chunk and run it.
24. Type iris[,c("Petal.Length","Sepal.Length")] into the chunk and run it.
25. Type iris[which(iris$Species=="versicolor"),] into the chunk and run it.
26. Type iris[which(iris$Species=="versicolor" & iris$Petal.Length>4.8),] into the chunk and run it.
27. Type iris[which(iris$Petal.Length==max(iris$Petal.Length)),] into the chunk and run it.
28. Read the help documentation on the which() function.
29. Type iris %>% filter(Species=="versicolor" & Petal.Length>4.8) into the chunk and run it.
30. Type iris %>% filter(Petal.Length==max(Petal.Length)) into the chunk and run it.
31. Type iris %>% mutate(Petal.Ratio=Petal.Length/Petal.Width,
Sepal.Ratio=Sepal.Length/Sepal.Width) into the chunk and run it.
32. Go to https:
dplyr.tidyverse.org/and figure out what you’ve done.
33. Write a short description of what you did/learned at the end of your Rmd file, after your chunk.
What does the “%>%” operator do in dplyr?
34. When you close RStudio, be sure to save any changes to your Rmd, but don’t save you
workspace.
Step 5: It’s Your Thing
1. Open your Rmd in RStudio.
2. Run your entire script from the top (Code > Run Region > Run All).
3. Create a new heading (“Step 5: It’s Your Thing”) and make a new chunk.
4. Type aggregate(Petal.Length ~ Species, data=iris, FUN="mean") into the chunk and run it.
5. Type aggregate(weight ~ feed, data=chickwts, FUN="mean") into the chunk and run it.
6. Read the help documentation on the aggregate() function.
7. Type iris %>% group_by(Species) %>% summarize(mean=mean(Petal.Length),
Answered Same Day Mar 22, 2023

Solution

Divya V answered on Mar 22 2023
33 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here