Import data

csv from local directory

The here package keeps directory structures sane. For what we will be doing, most of our calls with ‘here’ take the form: something <- read_something(here("directory", "filename"))

try1 <- read_csv(here("data", "dat.csv"))  #read in raw data from 'data' folder 
print(try1)
## # A tibble: 3 x 2
##   philadelphia harrisburg
##          <dbl>      <dbl>
## 1            1          4
## 2            2          5
## 3            3          6

csv from github

You need the readr package for this feat of greatness. remember to link to the ‘raw’ path.

step1 <- "https://github.com/Reilly-ConceptsCognitionLab/sandbox_scrapfiles/raw/main/fakedat.csv"
step2 <- read.csv(step1, header = T)
print(step2)
##   PhiLaDeLPHia.PA HArriSburg.PA
## 1               1             4
## 2               2             5
## 3               3             6


images, gifs, urls

gif

here’s a gif

urls

Here I am just typing along and all of a sudden I encounter a link to our lab when all of a sudden I encounter Bradley Boemke’s ‘harrypotter’ package. To knit this miraculous little snippet you would include [Bradley Boemke's 'harrypotter' package](https://github.com/bradleyboehmke/harrypotterpackage)

styling in html

fonts, size, styling of selection

Style the html output for text selections using inline css and metacharacters
1. **Bananas** is printed as Bananas
2. *Bananas* is printed as Bananas
3. <span style="color: #cc9900;font-size:30px">Bananas</span> is printed as Bananas

Sampling & distributions

Sample n-times from a specific range:
sample(x, size, replace = FALSE) where x is a range

dat <- sample(40:60, 10, replace = T)  #sample integers between 40-60, 10 observations, with replacement
print(dat)
##  [1] 48 43 40 42 53 40 60 51 50 51

Sample letters

Don’t worry about setting a seed here.

Samp1 <- sample(letters[1:5], 10, replace = T)  #sample letters between A and F, 20x with replacement
print(Samp1)
##  [1] "e" "b" "c" "a" "d" "b" "a" "b" "c" "c"
Samp2 <- sample(letters[1:26], 26, replace = F)  #sample letters A to Z, 26x no replacement
print(Samp2)
##  [1] "u" "p" "a" "i" "j" "d" "y" "c" "k" "z" "m" "e" "t" "h" "v" "r" "l" "g" "n"
## [20] "o" "x" "q" "f" "w" "b" "s"

Sample random character strings

set.seed(123)  #stri_rand_strings(n, length, pattern = '[A-Za-z0-9]')
weird <- stri_rand_strings(20, sample(3:7), pattern = "[A-Za-z0-9;#$*@!]")  #generate 20 strings between lengths 3-7 characters composed of any of the characters in this regex
print(weird)
##  [1] "sVPxO"   "eW2t"    "A$Gwsfb" "zcgUYD"  "6xt"     "em#Qj"   ";F@5"   
##  [8] "MMJ65@P" "Cq*Om4"  "W;4"     "jsJd2"   "KCnO"    "nnmNjag" "!Q;JZH" 
## [15] "3Ad"     "Ml2Ny"   "ss74"    "cHcF8l2" "PSYGRw"  "Qsu"

Sample from gaussian distribution

rnorm(n-samples, mean, sd) returns a sample of random normally distributed numbers

samp1 <- rnorm(20, 100, 2)  #10, mean=100, sd=2
print(samp1)
##  [1] 100.55205  97.90205  98.95826 103.24641  97.85986 103.37177  99.51662
##  [8]  99.06360  98.45404 104.29984  97.33129 100.99174 102.46795 101.26872
## [15] 100.82404 101.58717  99.69518  99.54221  98.19842  98.52995

Group and summarize

# tbd

Miscellaneous weird snippets

Lagging a vector

Some fake data. Create a new column that represents each observation minus the previous observation

set.seed(123)
a <- data.frame(stuff = sample(1:100, 30))
lagged <- a %>% mutate(diff = stuff - lag(stuff, 1))  #Subtract the previous observation from each new observation 
glimpse(lagged)
## Rows: 30
## Columns: 2
## $ stuff <int> 31, 79, 51, 14, 67, 42, 50, 43, 97, 25, 90, 69, 57, 9, 72, 26, …
## $ diff  <int> NA, 48, -28, -37, 53, -25, 8, -7, 54, -72, 65, -21, -12, -48, 6…