
Originally Posted by
PLogan
You lost your avatar. Looks naked. lol
Thanks for this code series.
I have enabled the options to hide avatars and signatures, so I forgot that I even had an avatar. It was the Komi sorcerer Šypiča (https://twitter.com/jurgenfug/status...03139905454080).
I have a library of over 2000 custom functions and aliases in my `.bashrc`, and almost all of them have 1-4 character names, so I can write shell commands in a much shorter than usual form. I have started to make a similar library for R. So for example with these lines from my R configuration files:
Code:
library(magrittr)
`|`=`%>%`
`%@%`=`%<>%`
amu=function(x)unname(as.matrix(x))
anu=function(x)unname(as.numeric(x))
ax=function(x,y)sapply(x,eval.parent(call("function",as.pairlist(alist(x=)),substitute(y))))
drn=function(x,...)x[!rownames(x)%in%c(...),]
euo=function(x,y)sqrt(outer(rowSums(x^2),rowSums(y^2),'+')-tcrossprod(x,2*y))
h5=function(x)head(x,2^5)
nr=nrow
o=order
p=function(...)writeLines(as.character(c(...)))
pas=paste
rc3=function(...)read.csv(...,row.names=1)
rn=rownames
rof=function(x,y)sprintf(paste0("%.",y,"f"),x)
spar=function(x,y)formatC(x,y,format="s")
swaw=function(w,x,y){x2=x;y2=y;y2[w]=x[w];x2[w]=y[w];e=parent.frame();do.call("=",list(substitute(x),x2),envir=e);do.call("=",list(substitute(y),y2),envir=e)}
ul=unlist
I can use this script to make two-way models where both source populations always have 50% ancestry:
Code:
source="g/25/mas"|rc3
targ="Udmurt"
target=source[targ,]|anu
source%@%drn(targ)
name=source|rn
source%@%amu
npop=source|nr
i1=2:npop|ax(x:npop)|ul
i2=1:(npop-1)|rep((npop-1):1)
points=(source[i1,]+source[i2,])/2
dist=points|euo(target|t)
ord=dist|o|h5
do=dist[ord]
n1=name[i1][ord]
n2=name[i2][ord]
swaw(n2<n1,n1,n2)
do|rof(3)|pas(n1|spar(n1|nchar|max),n2)|p
A regular R version of the script has about 58% more characters:
Code:
source=as.matrix(read.csv("g/25/mas",r=1))
targ="Udmurt"
target=unname(source[targ,])
source=source[!rownames(source)%in%targ,]
name=rownames(source)
source=unname(source)
npop=nrow(source)
ij1=unlist(lapply(2:npop,function(x)x:npop))
ij2=rep(1:(npop-1),(npop-1):1)
points=(source[ij1,]+source[ij2,])/2
dist=sqrt(outer(rowSums(points^2),sum(target^2),"+")-tcrossprod(points,2*t(target)))
ord=head(order(dist),32)
do=dist[ord]
n1=name[ij1][ord]
n2=name[ij2][ord]
n1s=pmin(n1,n2)
n2s=pmax(n1,n2)
writeLines(paste(sprintf("%.3f",do),formatC(n1s,max(nchar(n1s)),,"s"),n2s))