Skip to content

Instantly share code, notes, and snippets.

@hannesdatta
Created January 31, 2024 10:42
Show Gist options
  • Save hannesdatta/b33bcecf36c1300c4305531e1cc84647 to your computer and use it in GitHub Desktop.
Save hannesdatta/b33bcecf36c1300c4305531e1cc84647 to your computer and use it in GitHub Desktop.
Code from the first session of dPrep 2024 (https://dprep.hannesdatta.com)
# use cases
# as a calculator
x + 1
# to assign variables
x = 5
# calculation w/ variables
x + 5
# assign new values
y = x + 5
# assign new values w/ <-
y <- x + 7
# compare two objects
x == y
x
y
5 == (25/5)
# functions
x = c(1,2,3)
sum(x)
mean(x)
avg_x = mean(x)
round(8.42,1)
# getting help
?round
# assign character strings to a vector
cities = c('Amsterdam','Tilburg', 'Den Bosch')
# this doesn't run due to lack of quotation marks:
# cities = c(Amsterdam)
x = c('Hannes','Max','Lisa','Thijs')
x[1]
x[3]
x[x=='Lisa']
#### A BIT OF PRACTICE
# 1.
rooms = c(1,2,1,8)
# or:
rooms <- c(1,2,1,8)
# 2. the max function
max(rooms)
# 3.
rooms <- c(1,2,1,8,NA)
rooms <- c(rooms, NA)
# 4.
max(rooms)
?max
max_value <- max(rooms, na.rm = TRUE)
# 5.
rooms[3]
# 6.
rooms
rooms >= 2
which(rooms >= 2)
rooms[which(rooms >= 2)]
########
download.file("https://raw.githubusercontent.com/hannesdatta/course-dprep/master/content/docs/modules/week4/regional-global-daily-latest.csv", "streams.csv")
library(tidyverse)
streams <- read_csv('streams.csv', skip=1)
streams
head(streams)
view(streams)
nrow(streams)
summary(streams)
install.packages('tidyverse')
#
streams$Streams
mean(streams$Streams)
# '' for characters
# "" same, for characters
# `` for variable names containing spaces
streams %>% select(`Track Name`) # more modern way of coding in R
streams$`Track Name` # totally legacy and slow way of coding
streams %>% filter(`Track Name`=='Pepas')
streams %>% mutate(weekly_streams = `Streams` * 7)
###
streams %>% select(`Artist`,`Track Name`)
select(streams, `Artist`, `Track Name`)
### 2.
streams %>% filter(`Artist`=='Lil Nas X') %>% summarize(sum(`Streams`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment