Skip to content

Instantly share code, notes, and snippets.

@jrosell
jrosell / google_sheets_parse.txt
Last active March 22, 2023 13:41
When having Google Sheets in spanish or catalan, you can use these formulas to parse a texts to a proper date or number.
# From m/d/yyyy text to yyyy-mm-dd text.
=SUBSTITUTE(RIGHT(A4;4)&"-"&IF(LEN(A4)>9;LEFT(A4;2);"0"&LEFT(A4;1))&"-"&MID(A4;FIND("/";A4)+1;2);"/";"")
# From m/d/yyyy text to yyyy-mm-dd date.
=DATEVALUE(SUBSTITUTE(RIGHT(A4;4)&"-"&IF(LEN(A4)>9;LEFT(A4;2);"0"&LEFT(A4;1))&"-"&MID(A4;FIND("/";A4)+1;2);"/";""))
# From text 3.5 to 3,50 number
=VALUE(SUBSTITUTE(B5;".";","))
datos <- structure(list(Genero = c(2, 1, 1, 2, 1, 1, 1, 2, 2, 2),
`1. En casa` = c(1, 3, 1, 3, 4, 2, 4, 5, 5, 5),
`2. Con la tele` = c(4, 2, 1, 2, 3, 3, 2, 1, 1, 1)),
row.names = c(NA, -10L),
class = c("tbl_df", "tbl", "data.frame"))
# Opción con stats::wilcox.test
datos |>
dplyr::select(-Genero) |>
purrr::map_df(function(x){
```{python}
import csv
import requests
import time
# Open the CSV file and read the data
with open('data.csv', 'r') as file:
reader = csv.reader(file)
data = [row for row in reader]
@jrosell
jrosell / selenium_intercept_url.py
Last active January 20, 2023 18:49
Crawl a URL and capture network requests to third party resources like Google Analytics.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
@jrosell
jrosell / compose.py
Last active December 2, 2022 11:57
How to implement function composition in Python like we do in R: x <- 2; add_three <- \(x) x+3; duplicate <- \(x) x*2; x |> add_three() |> duplicate() |> {\(x) paste0("Result: ", x) }() # Result: 10
from typing import Callable
from functools import reduce, partial
add_three = lambda x: x+3
duplicate = lambda x: x*2
#-------
ComposableFunction = Callable[[float], float]
@jrosell
jrosell / polling.html
Created July 5, 2022 13:00
I currently have a tag that has a single trigger that is activated as many times as attributes need to be managed. I would like to add another tag that should be activated at the end, once the previous attributes have been processed by the initial tag.
<html><body>
<script>
function handle() {
console.log("handle")
window.myPolling = setTimeout(finish, 2000);
}
function finish() {
console.log("finish")
}
function fireAttribute(){
kgl_competitions_data_download <- function(competition_id, file_name, path = ".") {
.kaggle_base_url <- "https://www.kaggle.com/api/v1"
url <- paste0(.kaggle_base_url, "/competitions/data/download/", competition_id, "/", file_name)
rcall <- httr::GET(url, httr::authenticate(Sys.getenv("KAGGLE_USER"), Sys.getenv("KAGGLE_KEY"), type = "basic"))
tmp <- tempfile()
download.file(rcall$url, tmp)
invisible(file.copy(tmp, paste0(path, "/", file_name)))
}
#' Execute f for each row of df
#'
#' @param df data.frame
#' @param f function
#' @return A data.frame with the result of executin g for each row of df.
#' @examples
#' row_split_map(
#' tibble(x = c(1, 2), y = c(2, 3)),
#' function(df) {
#' df %>% mutate(
# Load libraries
library(tidyverse)
# Original names
df <- tibble(Q1=c(1), Q2=c(1), Q3=c(1), Q4=c(1), Q5=c(1), Q6=c(1), Q7=c(1), Q25_1=c(1), Q25_2=c(1), Q25_3=c(1), Q25_4=c(1), Q25_5=c(1), Q25_6=c(1), Q25_7=c(1), Q25_8=c(1), Q25_9=c(1), Q25_10=c(1), Q25_11=c(1), Q25_12=c(1), Q25_13=c(1), Q25_14=c(1), Q25_15=c(1), Q25_16=c(1), Q26_1=c(1), Q26_2=c(1), Q26_3=c(1), Q26_4=c(1), Q26_5=c(1), Q26_6=c(1), Q26_7=c(1))
df %>% glimpse()
# Option A) Using select and rename_with
df %>%
select(
@jrosell
jrosell / idescat-emex.R
Last active March 2, 2022 11:07
Obtenció de dades de "Municipi en xifres" de idescat via API en R.
library(tidyverse)
library(rvest)
library(xml2)
get_attrs <- function(df) {
ret <-
map(df, ~xml_attrs(df)) %>%
bind_rows %>%
rename(tipus = scheme)
ret