Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Created January 11, 2017 06:23
Show Gist options
  • Save mGalarnyk/2cda5835749f45eb29896d3f723882c6 to your computer and use it in GitHub Desktop.
Save mGalarnyk/2cda5835749f45eb29896d3f723882c6 to your computer and use it in GitHub Desktop.
library(datasets)
library(data.table)
iris_dt <- as.data.table(iris)
# 1 There will be an object called 'iris' in your workspace.
# In this dataset, what is the mean of 'Sepal.Length' for the species virginica? Please round your answer to the nearest whole number.
# Basic data.table syntax below .
#iris_dt[ essentially SQL Where class, select statement, groupby]
iris_dt[Species == "virginica",round(mean(Sepal.Length)) ]
# 3
# what is the absolute difference between the average horsepower of
# 4-cylinder cars and the average horsepower of 8-cylinder cars? (interest whole number)
mtcars_dt <- as.data.table(mtcars)
mtcars_dt <- mtcars_dt[, .(mean_cols = mean(hp)), by = cyl]
round(abs(mtcars_dt[cyl == 4, mean_cols] - mtcars_dt[cyl == 8, mean_cols]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment