Last active
December 29, 2015 22:49
-
-
Save wangyuchen/7738537 to your computer and use it in GitHub Desktop.
simple R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
res$dist <- unlist(lapply(c("Log-normal", "Gamma", "Uniform", "t"), rep, times=8)) | |
gl(n=4, k=8, labels=c("Log-normal", "Gamma", "Uniform", "t")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# x is data, h is frequency | |
x=1:8 | |
h=c(9,11,9,9,8,7,6,6) | |
unlist(apply(cbind(x,h),1,function(x){rep(x[1], x[2])})) | |
rep(x,h) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
revise.time <- function(df, date.ind='日期', time.ind='时间', test.ind='测试') { | |
# 函数声明中,函数名应尽量有意义(用动词) | |
# 参数设置时,尽量使用更少的参数,设默认值。这里需要一整个数据框和其中的三列, | |
# 并不需要把三列变量作为参数,仅需要指出他们是第几列,或者列名是什么即可。 | |
# 粘贴日期时间,转还类型 | |
time <- as.POSIXct(paste(df[[date.ind]], df[[time.ind]])) | |
# 根据测试号(1-6)修正时间 | |
time <- time - (6 - df[[test.ind]]) * 10 | |
# 按新的时间变量排列整个表 | |
return(cbind(time, df)[order(time), ]) | |
} | |
hfwd[order(hfwd$日期, hfwd$时间, hfwd$测试),] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment