博文

ifelse sqlite

library(DBI) library(openxlsx) library(tidyverse) library(magrittr) library(hablar) con <- dbConnect(RSQLite::SQLite(), "~/Downloads/phone.db") dbListTables(con) phones <- dbGetQuery(con, "select * from phones") #phones <- tbl(con,sql("select * from phones")) %>% collect() regions <- dbGetQuery(con, "select * from regions") phones %<>%   left_join(regions,by=c('region_id'='id')) %>%   select(number,province,city,zip_code,area_code) dbDisconnect(con) df2015 <- read.xlsx('./Downloads/甘肃全国12320数据至-20200223.xlsx',sheet = 1) %>%   mutate(year=2015) df2016 <- read.xlsx('./Downloads/甘肃全国12320数据至-20200223.xlsx',sheet = 2) %>%   mutate(year=2016) df2017 <- read.xlsx('./Downloads/甘肃全国12320数据至-20200223.xlsx',sheet = 3) %>%   mutate(year=2017) df2018 <- read.xlsx('./Downloads/甘肃全国12320数据至-20200223.xlsx',sheet = 4) %>%   mutate(year=2018) df...

hablar 类型转换

library(hablar) mtcars %>%   convert(num(gear),           chr(mpg),           fct(cyl)) df <- data.frame(a = c("A", NA, "B", "C", "C"),                  b = c(NA, 1, 1, 3, 3),                  c = c(7, 8, 2, 3, 3),                  stringsAsFactors = FALSE) # Returns duplicated rows df %>% find_duplicates() df %>% find_duplicates(b:c) df %>% find_na(b)

slider 行

library(slider) library(lubridate) library(tidyverse) x <- c(1, 2, 3, 4, 5) # .before: How many elements before the current one should be included in the window? # .after: How many elements after the current one should be included in the window? # .complete: Should .f only be evaluated when there is enough data to make a complete window?ff # .step: The number of elements to shift forward between calls to .f. slide_vec(x, mean, .before = 1) slide_vec(x, mean, .after = 1) slide_vec(x, sum, .before = 2) slide_vec(x, sum, .before = 2, .complete = T) index_vec <- as.Date("2019-08-29") + c(0, 1, 5, 6) wday_vec <- as.character(wday(index_vec, label = TRUE)) sales_vec <- c(2, 4, 3, 5) company <- tibble(sales = sales_vec,                   index = index_vec,                   wday = wday_vec) # Over columns: map(company, ~ .x) # Over rows: # slide(company, ~ .x) ...

代理测试

http代理 curl -x 127.0.0.1:10809 https://google.com sock5代理 curl --socks5 127.0.0.1:10808 https://google.com/

pip upgrade all

import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True) ----- pip install -U $(pip freeze | awk '{split($0, a, "=="); print a[1]}') ---- pip freeze > pip_frozen.txt pip install -r pip_frozen.txt --upgrade

Python pip配置国内源

1、 Linux平台安装方式: (1)创建 pip.conf 文件  首先运行以下命令 cd ~/.pip # 运行此命令切换目录  如果提示目录不存在,自行创建一个(如果目录存在,可跳过此步),如下: mkdir ~/.pip cd ~/.pip  在 .pip 目录下创建一个 pip.conf 文件,如下: touch pip.conf (2)编辑 pip.conf 文件  首先打开文件,命令如下: sudo vi ~/.pip/pip.conf  接着,写入以下内容: [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host = https://pypi.tuna.tsinghua.edu.cn # trusted-host 此参数是为了避免麻烦,否则使用的时候可能会提示不受信任 然后保存退出即可。  2、Window平台安装方式: (1)新建  pip  配置文件夹,直接在user用户目录中创建一个名为 pip  的文件夹( 即 %HOMEPATH%\pip ),如下图所示: (2)接着在 pip 文件夹中创建一个名为 pip 的文本文件(后缀名由" .txt "改为 " .ini "),格式如下所示:  文件内容如下: [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host = https://pypi.tuna.tsinghua.edu.cn # trusted-host 此参数是为了避免麻烦,否则使用的时候可能会提示不受信任 修改完成后保存,启动 cmd ,使用 " pip install xxx "( xxx 为你要下载的包名),即可默认使用国内源下载。

across and case_when

library(tidyverse) #across将一个或多个函数应用在选择列上 iris %>% as_tibble() %>%   mutate(mean=(rowMeans(across(starts_with('Sepal'))))) iris %>%   group_by(Species) %>%   summarise(across(starts_with('Sepal'),mean)) iris %>%   group_by(Species) %>%   summarise(across(starts_with('Sepal'),~mean(.x,na.rm = T))) #避免了写多个列的麻烦 iris %>%   group_by(Species) %>%   summarise(mean=mean(Sepal.Length)) starwars %>%   select(name:mass, gender, species) %>%   mutate(     type = case_when(       height > 200 | mass > 200 ~ "large",       species == "Droid"        ~ "robot",       TRUE                      ~ "other"     )   )