Skip to content

Instantly share code, notes, and snippets.

View verajosemanuel's full-sized avatar
💭
RRRRRRRRRRRRRRRR

Jose Manuel Vera verajosemanuel

💭
RRRRRRRRRRRRRRRR
View GitHub Profile
@verajosemanuel
verajosemanuel / gen_Dates.R
Created November 14, 2018 14:00
generate a #sequence of #dates in #R
seq(from = as.Date("2012/1/1"), to = as.Date("2019/1/1"), by = "month")
@verajosemanuel
verajosemanuel / kill_conn.R
Created October 26, 2018 12:03
#kill #connections to #database in #R
dbListConnections( dbDriver( drv = "MySQL"))
lapply( dbListConnections( dbDriver( drv = "MySQL")), dbDisconnect)
@verajosemanuel
verajosemanuel / own_sql.R
Created October 26, 2018 10:15
own #sql in #dbplyr
https://stackoverflow.com/questions/50661862/how-to-use-custom-sql-function-in-dbplyr
You can build your own SQL functions in R. They just have to produce a string that is a valid SQL query. I don't know the Jaro-Winkler distance, but I can provide an example for you to build from:
union_all = function(table_a,table_b, list_of_columns){
# extract database connection
connection = table_a$src$con
sql_query = build_sql(con = connection,
@verajosemanuel
verajosemanuel / rename_just_one.R
Created October 16, 2018 10:39
#rename just one #dataframe column
colnames(df)[1] <- "New name"
@verajosemanuel
verajosemanuel / wide_wide.R
Created October 16, 2018 10:31
#wide #dataframe with multiple measures in #R
library("reshape2")
my.df <- data.frame(ID=rep(c("A","B","C"), 5), TIME=rep(1:5, each=3), X=1:15, Y=16:30)
dcast(melt(my.df, id.vars=c("ID", "TIME")), ID~variable+TIME)
@verajosemanuel
verajosemanuel / flatten_df.R
Last active October 16, 2018 10:29
#flatten #dataframe to avoid nulls in #R
library(dplyr)
library(tidyr)
df <- data.frame(A=c(1,1),B=c(NA,2),C=c(3,NA),D=c(NA,2),E=c(5,NA))
df2 <- df %>%
group_by(A) %>%
fill(everything(), .direction = "down") %>%
fill(everything(), .direction = "up") %>%
slice(1)
@verajosemanuel
verajosemanuel / export_xaringan_pdf.R
Created October 8, 2018 11:46
#export #xaringan #presentation to #pdf
library(webshot)
file_name <- normalizePath("C://Users//username//Documents//prez.html")
webshot(file_name, "prez.pdf")
@verajosemanuel
verajosemanuel / regex_two_lines.reg
Created October 4, 2018 14:25
#regex to get a pattern two lines and newline
name_name_name_id_seq
AS integer
START WITH 1
INCREMENT BY 1
_lastword\s*AS integer\s*START WITH
AS integer(?=\sSTART WITH)
# to replace
@verajosemanuel
verajosemanuel / remove_non_numbers.R
Created October 4, 2018 09:43
#remove any non number character from vector in #R
readr::parse_number("ads052788R")
@verajosemanuel
verajosemanuel / df_from_df.R
Last active October 29, 2018 15:25
generate #dataframe where #columns are values from other in #R
df2 <- read.table(text = "", col.names = df1$name)