-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ScHARR-PHEDS/custom_functions
session 6 on new branch
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
rsconnect/documents/index.rmd/beta.rstudioconnect.com/rasmith3/introduction_to_r.dcf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: introduction_to_r | ||
title: Introduction to R | ||
username: rasmith3 | ||
account: rasmith3 | ||
server: beta.rstudioconnect.com | ||
hostUrl: https://beta.rstudioconnect.com/__api__ | ||
appId: 18401 | ||
bundleId: 87526 | ||
url: https://beta.rstudioconnect.com/content/69e47c65-9ed8-4c8e-8c1c-274047c2dc29/ | ||
when: 1647983059.62124 | ||
lastSyncTime: 1647983059.62124 | ||
asMultiple: FALSE | ||
asStatic: TRUE |
11 changes: 11 additions & 0 deletions
11
rsconnect/documents/index.rmd/rpubs.com/rpubs/Document.dcf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: Document | ||
title: | ||
username: | ||
account: rpubs | ||
server: rpubs.com | ||
hostUrl: rpubs.com | ||
appId: https://api.rpubs.com/api/v1/document/880998/4a51f3b0134e448b92fb995ba3bb1f99 | ||
bundleId: https://api.rpubs.com/api/v1/document/880998/4a51f3b0134e448b92fb995ba3bb1f99 | ||
url: http://rpubs.com/publish/claim/880998/2dcf3454f3824927ab0a0e9ebad6d706 | ||
when: 1647983071.21339 | ||
lastSyncTime: 1647983071.21339 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#============= | ||
# R4ScHARR | ||
# Robert Smith, Paul Schneider & Sarah Bates | ||
# Session 6 Script | ||
#============= | ||
|
||
|
||
#====================# | ||
# For loops ---- | ||
#====================# | ||
|
||
# For loops can be used to repeat the same action multiple times without lots of | ||
# lines of code. This is a simple function to print list of numbers | ||
for(i in 1:10) { | ||
|
||
print(i) | ||
} | ||
|
||
# It's useful to when applying a function to all rows or columns of a dataset | ||
|
||
# recreate simple data frame used previously | ||
|
||
height <- c(1.38,1.45,1.21,1.56) # combine is used to create the vector. | ||
weight <- c(45,42,43,50) | ||
first_name <- c("Alice","Bob","Harry","Jane") | ||
sex <- factor(x = c("F","M","M","F")) | ||
df <- data.frame(height,weight,first_name,sex) | ||
df$bmi <- df$weight / df$height^2 | ||
|
||
# use the for loop to report each persons height | ||
for (i in 1:nrow(df)) { | ||
|
||
# print combination of the value in column 3 (first name) with the value in column 1 (height) | ||
print(paste0(df[i,3], " is ",df[i,1], "m tall")) | ||
|
||
} | ||
|
||
# use the for loop to change the variable name (column) | ||
|
||
for(i in 1:ncol(df)) { | ||
|
||
# change the name of each column to var followed by the column number | ||
colnames(df)[i] <- paste0("Var_", i) | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
#====================# | ||
# 4.2 Custom functions ---- | ||
#====================# | ||
|
||
# Creating a custom function can be really useful - reduces lines of code and makes work | ||
# easily reproducible, clearer and more organised | ||
|
||
# The structure of a function is: | ||
|
||
# function_name <- function(arg1, arg2, ... ){ | ||
# statements | ||
# return(object) | ||
# } | ||
|
||
# Here's a simple example | ||
|
||
f_simple <- function(x,y,z){ | ||
|
||
value = x + y - z | ||
|
||
return(value) | ||
|
||
} | ||
|
||
# to see the details of the function you can just run the name | ||
f_simple | ||
|
||
# to run the function, add the argument in brackets | ||
|
||
f_simple(1,2,3) | ||
f_simple(6,5,12) | ||
|
||
|