This file contains hidden or 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
require(tibble) | |
# if nrow is numerical | |
df <- rownames_to_column(df, var = "id") | |
# if nrow is not numerical | |
df$id <- seq.int(nrow(df)) |
This file contains hidden or 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
source('py_logging.R') | |
# Set up logging | |
logger.setup(debugLog = "debug.log", infoLog = "info.log", errorLog = "error.log") | |
# Silence other warning messages | |
options(warn = -1) # -1=ignore, 0=save/print, 1=print, 2=error | |
err_msg <- geterrmessage() | |
logger.error('Error on running some scripts: %s') |
This file contains hidden or 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
df <- read.csv("data.csv", sep = ";", stringsAsFactors = FALSE) | |
library("FFTrees") | |
## Recoding | |
pdf$recoded <- pdf$original | |
pdf$recoded[pdf$TIPO == "no"] <- "0" | |
pdf$recoded[pdf$TIPO == "yes"] <- "1" | |
pdf$recoded <- as.numeric(pdf$recoded) |
This file contains hidden or 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
mb <- microbenchmark(function_1(df), function_2(df), function_3(df), function_4(df)) | |
library(ggplot2) | |
autoplot(mb) |
This file contains hidden or 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
x <- c("m", "f", "u", "f", "f", "m", "m") | |
lookup <- c(m = "Male", f = "Female", u = NA) | |
lookup[x] | |
unname(lookup[x]) |
This file contains hidden or 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
grades <- c(1, 2, 2, 3, 1) | |
info <- data.frame( | |
grade = 3:1, | |
desc = c("Excellent", "Good", "Poor"), | |
fail = c(F, F, T) | |
) | |
# info table with grades |
This file contains hidden or 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
df <- data.frame(x = c(2, 4, 1), y = c(9, 11, 6), n = c(3, 5, 1)) | |
rep(1:nrow(df), df$n) | |
df[rep(1:nrow(df), df$n), ] |
This file contains hidden or 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
# discard columns | |
df[setdiff(names(df), "y")] | |
(x <- c(sort(sample(1:20, 9)), NA)) | |
(y <- c(sort(sample(3:23, 7)), NA)) | |
union(x, y) | |
intersect(x, y) |
This file contains hidden or 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
for (i in 1:4) { | |
assign(paste("df_name",i,sep = ""), read.csv(paste0("df",i,".csv"), sep = ";", stringsAsFactors = FALSE)) | |
} |
This file contains hidden or 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
library(janitor) | |
df <- remove_empty_rows(df) | |
df <- remove_empty_cols(df) | |
# clean names | |
names(df) <- names(clean_names(df)) | |
OlderNewer