Skip to content

Instantly share code, notes, and snippets.

View leoluyi's full-sized avatar
🎯
Focusing

Leo Lu leoluyi

🎯
Focusing
View GitHub Profile
@leoluyi
leoluyi / CA_functions.R
Last active September 4, 2015 07:58
對應分析
## === 第一次用R需安裝套件 ===
list.of.packages <- c("ggplot2", "xlsx", "FactoMineR","showtext",
"Cairo", "ggthemes","directlabels","dplyr")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
# 內建圖
ca_plot <- function (df,label=c("印象","品牌"),textSize=10,
x_limits=c(-0.5,0.6),.alpha=0.7,
@leoluyi
leoluyi / 食安recode.R
Last active August 29, 2015 14:08
食安變數合併(recode)
library(data.table)
library(xlsx)
library(dplyr)
library(pipeR)
library(car)
## 讀取資料
fileNameA <- '3337_A卷.csv' # (手動更改參數)
fileNameB <- '3371_B卷.csv' # (手動更改參數)
# options(java.parameters = "-Xmx16g") # read.xlsx增加記憶體
## === 第一次用R需安裝套件 ===
list.of.packages <- c("plyr", "dplyr", "sjmisc")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
rm(list.of.packages, new.packages)
combine_pipe_table <- function (
.data, .var_start, .var_end, .MR_num, .step)
{
# .var_start # 表格起始變數名
@leoluyi
leoluyi / recode_to.R
Last active August 29, 2015 14:10 — forked from hadley/recode.R
# forked from hadley/recode.R
# https://gist.github.com/hadley/2751ba61d1c7f4eaacab
recode_to <- function(df, ..., match = c("first", "last")) {
match <- match.arg(match)
cases <- lapply(list(...), as.case)
if (identical(match, "last")) cases <- rev(cases)
n <- nrow(df)
write.Hmisc.SPSS = function(data, datafile, codefile) {
## Write an SPSS file from R with variable labels from the Hmisc package
# source:
# http://stackoverflow.com/questions/10181730/information-from-label-attribute-in-r-to-variable-labels-in-spss/10261534#10261534
# EXAMPLE DATA (see: http://stackoverflow.com/q/10181730/1270695)
#
# If you do not want to alter your original file, as in the example above,
# and if you are connected to the internet while you are using this function,
# you can try this self-contained function:
## === 第一次用R需安裝套件 ===
list.of.packages <-
c("plyr", "xlsx", "tools","dplyr","openxlsx",
"sampling", "magrittr", "sjmisc")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
rm(list.of.packages, new.packages)
## -- 更新 package ----
if(packageDescription("openxlsx")$Version < "2.3.11") {
### Using the Windows Clipboard, or Passing Data Quickly From Excel to R and Back Again
## http://www.r-bloggers.com/using-the-windows-clipboard-or-passing-data-quickly-from-excel-to-r-and-back-again/
# Copy data out of R
copy.table <- function(obj, size = 4096) {
clip <- paste('clipboard-', size, sep = '')
f <- file(description = clip, open = 'w')
write.table(obj, f, row.names = FALSE, sep = '\t')
close(f)
}
multi.freq.table = function(data, sep="+", dropzero=FALSE, clean=TRUE) {
## 共選分析
# Takes boolean multiple-response data and tabulates it according
# to the possible combinations of each variable.
#
# See: http://stackoverflow.com/q/11348391/1270695
counts = data.frame(table(data))
N = ncol(counts)
counts$Combn = apply(counts[-N] == 1, 1,
write.spss <-
function(data, datafile, codefile, varlabels=NULL) {
# EXAMPLE DATA (see: http://stackoverflow.com/q/10181730/1270695)
#
# If you do not want to alter your original file, as in the example above,
# and if you are connected to the internet while you are using this function,
# you can try this self-contained function:
#
# df <- data.frame(id = c(1:6),
# p.code = c(1, 5, 4, NA, 0, 5),
@leoluyi
leoluyi / combine_pipe_table.R
Created December 11, 2014 10:17
EOL資料清整工具 - 合併表格重複piping
combine_pipe_table <- function (
.data, .var,
start = grep(.var, colnames(.data)),
Qcount, MRnum, step, key)
{
# .var # 表格起始變數名
# start # 開始欄
# Qcount # piping來源題數
# MRnum # 複選題數
# step # piping相同題數