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 / nested_json_to_df.R
Created October 11, 2019 15:11
#nested #json to #dataframe in #R
a <- jsonlite::fromJSON("raw_data/countries.json")
DT <- data.table::rbindlist(a)
@verajosemanuel
verajosemanuel / edit_rprofile.R
Created September 10, 2019 09:50
edit #rprofile in #R
usethis::edit_r_profile()
@verajosemanuel
verajosemanuel / last_day_sequence.R
Created July 30, 2019 13:52
last day of month #date #sequence in base #R
seq(as.Date("2010-02-01"), length=24, by="1 month") - 1
@verajosemanuel
verajosemanuel / R_install_root_package.R
Created May 31, 2019 11:24
#install #package #root #linux #R
sudo su - -c "R -e \"install.packages('dplyr', repos='http://cran.rstudio.com/')\""
@verajosemanuel
verajosemanuel / appendSQL_R.R
Created May 17, 2019 10:50
#append data to a #PostgreSQL #table with `dplyr` without `collect()` in #R
dplyr does not include commands to insert or update records in a database, so there is not a complete native dplyr solution for this. But you could combine dplyr with regular SQL statements to avoid bringing the data to R.
Let's start by reproducing your steps before the collect() statement
library(dplyr)
pg <- src_postgres()
reg_data <- tbl(pg, "reg_data")
@verajosemanuel
verajosemanuel / mailErr.R
Created May 17, 2019 10:16
#mail #error in #R
options(error = function() {
subject.msg <- paste0("Error Subject, ", format(Sys.time(), "%A %d %B %Y %X"))
from <- "sender@mail"
body <- geterrmessage()
mailControl = list(smtpServer = "mail.server.com", smtpPort = "25")
sendmail(from = from,
to = c("[email protected]","[email protected]"),
subject = subject.msg,
msg = body,
@verajosemanuel
verajosemanuel / extract_date_ts.R
Created April 24, 2019 07:58
extract #dates from #timeseries object in #R
transform(df, index = as.Date(index, frac = 1))
@verajosemanuel
verajosemanuel / collapse_list_of_df.R
Created April 12, 2019 11:25
#collapse a #list of #dataframes in #R
bind_rows(list_of_dataframes, .id = "column_label")
@verajosemanuel
verajosemanuel / print_index_lapply.R
Created December 17, 2018 16:06
#print #index in #lapply #function in #R
myfunction<- function(values, index){
cat("Adding values (", index, "): ", values[1], "...", values[2], " = ", sum(values), "\n" )
invisible(values[1] + values[2])
}
mylist <- list(c(5, 4), c(3, 2), c(1, 3))
mapply(myfunction, mylist, seq_along(mylist))
@verajosemanuel
verajosemanuel / get_pos_name.R
Created November 14, 2018 15:51
get #column #index #position by #name in #R #dataframe
grep("^name$", colnames(df))