# Tips

## 2017/09/15

Categories: Featured Tags: mathematics R bash

Here I present some random R, Git, Bash tips and tricks I have learned from various resources. (Stackoverflow, Google, Wikia etc

# R

## Remove columns with all NAs or all blank values

naRemove <- function(df) Filter(function(x) !all(is.na(x)) & !all(trimws(x) == ''), df)


## switch

If value is a character vector then the element of ‘…’ with a name that exactly matches value is evaluated. If there is no match a single unnamed argument will be used as a default. If no default is specified, NULL is returned.

> y <- "fruit"
> switch(y, fruit = "banana", vegetable = "broccoli", "Neither")
[1] "banana"
> y <- "meat"
> switch(y, fruit = "banana", vegetable = "broccoli", "Neither")
[1] "Neither"
A common use of switch is to branch according to the character value of one of the arguments to a function.

> centre <- function(x, type) {
+ switch(type,
+        mean = mean(x),
+        median = median(x),
+        trimmed = mean(x, trim = .1))
+ }
> x <- rcauchy(10)
> centre(x, "mean")
[1] 0.8760325
> centre(x, "median")
[1] 0.5360891
> centre(x, "trimmed")
[1] 0.6086504


Reference: R language definition

## stars.pval {gtools}

This will convert p-value to stars (*).

stars.pval <- function (p.value) {
unclass(symnum(p.value, corr = FALSE, na = FALSE, cutpoints = c(0,
0.001, 0.01, 0.05, 0.1, 1), symbols = c("***", "**",
"*", ".", " ")))
}


## identifying os

get_os <- function(){
sysinf <- Sys.info()
if (!is.null(sysinf)){
os <- sysinf['sysname']
if (os == 'Darwin')
os <- "osx"
} else { ## mystery machine
os <- .Platform$OS.type if (grepl("^darwin", R.version$os))
os <- "osx"
if (grepl("linux-gnu", R.version$os)) os <- "linux" } tolower(os) }  Reference: conjugateprior.org ## math styles in R plots demo(plotmath)  ## Converting a matrix of characters into numeric mat <- matrix(c("5","6","7","8","hello","world"),ncol=3) class(mat) <- "numeric"  # Git ## submodule git clone https://github.com/aikiframework/json.git --recursive git submodule update --init # fetch submodules if you already cloned but forgot --recursive  Reference: Stackoverflow ## Reducing size git reflog expire --expire=now --all git gc --prune=now git gc --aggressive --prune=now  Reference: npteam ## Github open # Opens the github page for the current git repository in your browser # git@github.com:jasonneylon/dotfiles.git # https://github.com/jasonneylon/dotfiles/ function gh() { giturl=$(git config --get remote.origin.url)
if [ "$giturl" == "" ] then echo "Not a git repository or no remote.origin.url set" exit 1; fi giturl=${giturl/git\@github\.com\:/https://github.com/}
giturl=${giturl/\.git/\/tree/} branch="$(git symbolic-ref HEAD 2>/dev/null)" ||
branch=${branch##refs/heads/} giturl=$giturl$branch open$giturl
}


## Git initializing

echo "# REPONAME" >> README.md
git init
git commit -m "first commit"
git push -u origin master


## .gitignore

Ignore 되어야 하지만 이미 add된 파일 표시

git ls-files -ci --exclude-standard


Ignore 되어야 하지만 이미 add된 파일을 GIT에서 제거 (파일 자체는 남겨둠.)

git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached


## Syncing a fork

git remote add --track master upstream https://github.com/mmistakes/so-simple-theme.git
git fetch upstream
git merge upstream/master --allow-unrelated-histories


Reference: Dogfeet

## Diagram

Reference: bunhere.tistory.com

# Bash

## Convert PDF to PNG - multiple files

for file in *_PK_Model_DV_vs_PRED.pdf; do
convert -density 300 "$file" "${file%.pdf}.png"
done


## Replace string across directories

grep -rl 'NonCompart' * | xargs sed -i '' -e 's/NonCompart/ncar/g'


## Lazygit : git add, commit, push for lazy people like me.

Copy & paste this lines to your ~/.bash_profile

function lazygit() {
git commit -a -m "$1" git push }  lazygit "Update README.md"  # Vim ## Dos > Unix conversion se ff=unix  Reference: mwultong ## find all the word ‘foo’ where there is no ‘bar’ following, we can do: /foo$$.*bar$$\@!  Reference: Wikia ## Word count %s/[^ ]\+//gn  Reference: Wikia ## Title case s/\<$$\w$$$$\w*$$\>/\u\1\L\2/g  Reference: Wikia ## Quote 줄의 앞뒤로 따옴표 붙이기 %s/^$$.*$$$/"\1"/

%s/.*/"&"


줄의 맨 뒤로 따옴표 붙이기

%s/^$$.*$$$/\1"/  Bash cat foo.txt | sed s/^$$.*$$$/"\1"/g


Reference: Stackoverflow

## Markdown 에서 bullet 맨 앞에 대문자로

:%s/$$- [a-z]$$/\U\1/


Reference: Wikia

# VB

## Repeat Header Row in Word

Sub RepeatTableHeadings()

Dim tbl As Table

For Each tbl In ActiveDocument.Tables
Next tbl

End Sub


Reference: BeyondVBA

## Image resize

Sub resize()
Dim i As Long
With ActiveDocument
For i = 1 To .InlineShapes.Count
With .InlineShapes(i)
.ScaleHeight = 24
.ScaleWidth = 24
End With
Next i
End With
End Sub


Reference: superuser