forked from AnnaWilliford/SWC_Fall2019_test
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathRprogrammingTask.Rmd
More file actions
133 lines (108 loc) · 3.77 KB
/
RprogrammingTask.Rmd
File metadata and controls
133 lines (108 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
---
title: "R: practice writing scripts: for, if, user-defined funcions, ggplot2"
author: "Anna Williford"
date: "9/12/2019"
output: html_document
---
I was given `gapminder.txt` dataset and asked to do the following:
**For every unique country in gapminder.txt dataset, if the country starts with 'Ma', use ggplot2 package to plot life expectancy(y axis) over years(x axis).**
Here are the steps I need to implement in R to complete this task.
1. Load `ggplot2` library
2. Load `gapminder.txt`
3. Create `country_list` that contains country names from `gapminder.txt`.
4. Start a `for` loop to iterate over every element of `country_list`
5. Use `if` statement to check if the country name starts with "Ma".
6. If it does, subset gapminder dataset to select information about just that country.
7. Make ggplot to plot life expectancy on y-axis and years on x-axis using subset of the data from step 6.
8. Close `if`statement and `for` loop.
Here is a scaffold for a programm:
```
#1 load library
library(ggplot2)
#2 load data
gapminder<-read.table("gapminder.txt", header = TRUE)
#3 create list of countries to loop over
country_list=
#4 loop over country_list
for(country in country_list){
#5 if(country starts with "Ma"){
#5 subset gapminder
#6 make ggplot
}
}
```
Here is the implementation of these steps:
```{r, plots}
#load packages
library(ggplot2)
#read data ito R
gapminder<-read.table("gapminder.txt", header = TRUE)
#make country_list to loop over
country_list=unique(gapminder$country)
#loop over counry_list
for(country in country_list) {
if(startsWith(country,"Ma")){
#subset data to get info about the country if its name starts with 'Ma'
countryData<-gapminder[gapminder$country==country,]
#make ggplot with countryData
print(ggplot(data=countryData)+aes(x=year,y=lifeExp)+geom_line(color="red")
+labs(title=country))
}
}
```
If you want to save each plot to a separate file, make a unique name for every plot and save with `ggsave`
```{r, loop_if_plot, eval=FALSE}
#load packages
library(ggplot2)
#read data ito R
gapminder<-read.table("gapminder.txt", header = TRUE)
#make country_list to loop over
country_list=unique(gapminder$country)
#loop over counry_list
for(country in country_list) {
if(startsWith(country,"Ma")){
#subset data to get info about the country if its name starts with 'Ma'
countryData<-gapminder[gapminder$country==country,]
#make ggplot with countryData
myplot<-ggplot(data=countryData)+aes(x=year,y=lifeExp)+geom_line(color="red")
#create a unique file name to save plot to
f_name=paste0(country,"_plot.png")
#save plot
ggsave(myplot, filename=f_name)
}
}
```
**Note** The script would be easier to read if subsetting was done with a user-defined function:
```{r, subsetting function, eval=FALSE}
getCountryData<-function(countryName, df){
countryData<-df[df$country==countryName,]
return(countryData)
}
```
Then, we can rewrite the above script like this:
```{r, loop_if_plot_withFunction, eval=FALSE}
#load packages
library(ggplot2)
#define functions
getCountryData<-function(countryName, df){
countryData<-df[df$country==countryName,]
return(countryData)
}
#read data ito R
gapminder<-read.table("gapminder.txt", header = TRUE)
#make country_list to loop over
country_list=unique(gapminder$country)
#loop over counry_list
for(country in country_list) {
if(startsWith(country,"Ma")){
#subset data to get info about the country if its name starts with 'Ma'
countryData<-getCountryData(country,gapminder)
#make ggplot with countryData
myplot<-ggplot(data=countryData)+aes(x=year,y=lifeExp)+geom_line(color="red")
#create a unique file name to save plot to
f_name=paste0(country,"_plot.png")
#save plot
ggsave(myplot, filename=f_name)
}
}
```