Skip to content

Instantly share code, notes, and snippets.

@leoluyi
Created December 11, 2014 10:17
Show Gist options
  • Save leoluyi/16d793ed90c4fdce8029 to your computer and use it in GitHub Desktop.
Save leoluyi/16d793ed90c4fdce8029 to your computer and use it in GitHub Desktop.
EOL資料清整工具 - 合併表格重複piping
combine_pipe_table <- function (
.data, .var,
start = grep(.var, colnames(.data)),
Qcount, MRnum, step, key)
{
# .var # 表格起始變數名
# start # 開始欄
# Qcount # piping來源題數
# MRnum # 複選題數
# step # piping相同題數
# key # 關鍵字
library(dplyr)
# 防呆
is.integer0 <- function(x) {is.integer(x) && length(x) == 0L}
if(is.integer0(start)) stop(c("找不到變數: ",.var))
for(i in seq(1,(MRnum*step)*(Qcount-1)+1, by=step*MRnum)) {
j <- start + i -1
for(k in 0:(MRnum -1)){
cat(i %/% (step*MRnum) + 1,
colnames(.data)[j+k],'<<',colnames(.data)[j+k+MRnum],"\n")
.data[,j+k] <-
ifelse(.data[,j+k+MRnum]==key, .data[,j+k+MRnum], .data[,j+k])
}
}
return(.data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment