Title: | Julian Miscellaneous Function |
---|---|
Description: | Some handy function in R. |
Authors: | TszKin Julian Chan <[email protected]> |
Maintainer: | TszKin Julian Chan <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.3.1.1 |
Built: | 2025-03-01 04:51:06 UTC |
Source: | https://github.com/cran/Jmisc |
Paste two strings together without separation.
s1 %+% s2
s1 %+% s2
s1 |
First String |
s2 |
Second String |
paste(s1,s2,sep="")
TszKin Julian Chan [email protected]
cat("Hello" %+% "World")
cat("Hello" %+% "World")
Add a constant column to data.frame or matrix.
addCol(x, ..., value)
addCol(x, ..., value)
x |
|
... |
constants |
value |
|
a data.frame
or matrix
contains all columns
in x and those constant columns.
TszKin Julian Chan [email protected]
d=data.frame(x=1:5,y=11:15) addCol(d,a=1,b=2,c=3) addCol(d,value=c(a=100,b=200,c=300))
d=data.frame(x=1:5,y=11:15) addCol(d,a=1,b=2,c=3) addCol(d,value=c(a=100,b=200,c=300))
Demean a vector or a matrix (by column)
demean(x)
demean(x)
x |
Vector or matrix |
Demeaned value of x
TszKin Julian Chan [email protected]
x<-matrix(1:20,ncol=2) demean(x)
x<-matrix(1:20,ncol=2) demean(x)
This function evaluates a function x
under an
environment which is created by a list. All elements of the
list is local to the function; other words all elements of
the list can be accessed directly by the function. A new
environment is created and each element of variables
is assigned to the new environment. Then the environment
associated with the x
is updated with the new
environment. Finally x(...)
is evaluated and return
the result.
evalFunctionOnList(x, variables = list(), ..., parent_env)
evalFunctionOnList(x, variables = list(), ..., parent_env)
x |
A function to be called |
variables |
A list to be converted to an environment |
... |
Further arguments to |
parent_env |
parent environment |
Return value of the x(...)
.
TszKin Julian Chan [email protected]
evalFunctionOnList(function() rnorm(n,mean,sd),list(n=5,mean=5,sd=1))
evalFunctionOnList(function() rnorm(n,mean,sd),list(n=5,mean=5,sd=1))
Generate t-statistics, p-value and significance from estimates and its sd. Estimates and its SD is the first and second column respectively
generateSignificance(x, row_names)
generateSignificance(x, row_names)
x |
A matrix or data.frame |
row_names |
names of row |
a data.frame
TszKin Julian Chan [email protected]
n<-1000 x_data<-cbind(rnorm(n,mean=0),rnorm(n,mean=1)) x_estimates<-cbind(apply(x_data,2,mean),apply(x_data,2,sd)/sqrt(n)) generateSignificance(x_estimates) generateSignificance(x_estimates,row_names=c("mean0","mean1") )
n<-1000 x_data<-cbind(rnorm(n,mean=0),rnorm(n,mean=1)) x_estimates<-cbind(apply(x_data,2,mean),apply(x_data,2,sd)/sqrt(n)) generateSignificance(x_estimates) generateSignificance(x_estimates,row_names=c("mean0","mean1") )
Return the p Value of Jarque Bera test. The Jarque Bera test test the null hypothesis that the data are from a normal distribution.
JBTest(x)
JBTest(x)
x |
data |
p Value of Jarque Bera test
TszKin Julian Chan [email protected]
JBTest(rnorm(50)) JBTest(rt(50,3)) n=100 # size mean(replicate(n,JBTest(rnorm(100)))<0.05) # power mean(replicate(n,JBTest(rt(100,3)))<0.05)
JBTest(rnorm(50)) JBTest(rt(50,3)) n=100 # size mean(replicate(n,JBTest(rnorm(100)))<0.05) # power mean(replicate(n,JBTest(rt(100,3)))<0.05)
Combine label_both and label_parsed in ggplot2. Also added a rename function to it see label_both and label_parsed in ggplot2 for details.
label_both_parsed_recode(display_name)
label_both_parsed_recode(display_name)
display_name |
A vector contains the display name. Names of the vector are the original name. |
A function similar to label_both and label_parsed in ggplot2 for details.
TszKin Julian Chan [email protected]
https://CRAN.R-project.org/package=ggplot2
Outer apply It use the expand.grid to compute all possible
combination of X
and Y
, then call the mapply
with the combination generated and FUN
.
oapply(X, Y, FUN, switch_order = FALSE, ...)
oapply(X, Y, FUN, switch_order = FALSE, ...)
X |
first argument to |
Y |
second argument to |
FUN |
a function to apply. See mapply |
switch_order |
Switch the order of |
... |
other arguments to mapply |
same as mapply.
TszKin Julian Chan [email protected]
oapply(11:15,1:5,choose) oapply(11:15,1:5,choose,switch_order=TRUE)
oapply(11:15,1:5,choose) oapply(11:15,1:5,choose,switch_order=TRUE)
load add-on packages. If the packages can not be found, install.packages is called.
packages(x, ...)
packages(x, ...)
x |
name of the packages |
... |
arguments to install.packages |
TszKin Julian Chan [email protected]
## Not run: packages("foreach") ## End(Not run)
## Not run: packages("foreach") ## End(Not run)
Recode the value of a vector
or matrix
.
recode(x, from, to)
recode(x, from, to)
x |
a |
from |
original value of |
to |
new value of |
recoded x
TszKin Julian Chan [email protected]
x=rep(1:5,each=2) recode(x,from=1:5,to=5:1) recode(x,from=1:5,to=11:15)
x=rep(1:5,each=2) recode(x,from=1:5,to=5:1) recode(x,from=1:5,to=11:15)
Repeat a vector by col
repCol(x, n)
repCol(x, n)
x |
|
n |
number of replication |
TszKin Julian Chan [email protected]
repRow(c(a=1,b=2,c=3),5) repCol(c(a=1,b=2,c=3),5)
repRow(c(a=1,b=2,c=3),5) repCol(c(a=1,b=2,c=3),5)
Repeat a vector by row
repRow(x, n)
repRow(x, n)
x |
|
n |
number of replication |
TszKin Julian Chan [email protected]
repRow(c(a=1,b=2,c=3),5) repCol(c(a=1,b=2,c=3),5)
repRow(c(a=1,b=2,c=3),5) repCol(c(a=1,b=2,c=3),5)
shift_by
unitRepeat a vector by row
shift(x, shift_by)
shift(x, shift_by)
x |
a vector |
shift_by |
number of shift |
TszKin Julian Chan [email protected]
d<-data.frame(x=1:15) #generate lead variable d$df_lead2<-shift(d$x,2) #generate lag variable d$df_lag2<-shift(d$x,-2)
d<-data.frame(x=1:15) #generate lead variable d$df_lead2<-shift(d$x,2) #generate lag variable d$df_lag2<-shift(d$x,-2)
Source all file with extension .r or .R
sourceAll(path = ".", ...)
sourceAll(path = ".", ...)
path |
path of the directory |
... |
other arguments to source |
TszKin Julian Chan [email protected]
## Not run: sourceAll() ## End(Not run)
## Not run: sourceAll() ## End(Not run)
Split a vector by a sequence of length This function will
split the vector x
into length(x)
subvector.
The length of each subvector is given by by
.
splitBy(x, by)
splitBy(x, by)
x |
A vector to be splitted |
by |
A vector of length |
a list of subvector
TszKin Julian Chan [email protected]
splitBy((1:10)*10,c(2,2)) splitBy((1:10)*10,c(2,3,4)) ## Not run: expect_equivalent(splitBy((1:10)*10,c(2,2)) , list(c(10,20),c(30,40))) expect_equivalent(splitBy((1:10)*10,c(2,3,4)) , list( c(10,20), c(30,40,50) ,c(60,70,80,90) )) ## End(Not run)
splitBy((1:10)*10,c(2,2)) splitBy((1:10)*10,c(2,3,4)) ## Not run: expect_equivalent(splitBy((1:10)*10,c(2,2)) , list(c(10,20),c(30,40))) expect_equivalent(splitBy((1:10)*10,c(2,3,4)) , list( c(10,20), c(30,40,50) ,c(60,70,80,90) )) ## End(Not run)
Start/clock to measure performance. Same as tic and toc in matlab
tic(name = ".time_Jmisc", envir = .GlobalEnv) toc()
tic(name = ".time_Jmisc", envir = .GlobalEnv) toc()
name |
Name of the temporary time variable |
envir |
environment of the temporary time variable |
TszKin Julian Chan [email protected]
## Not run: tic() Sys.sleep(1) toc ## End(Not run)
## Not run: tic() Sys.sleep(1) toc ## End(Not run)