我寫了一個函數,它接受任何DataFrame並評估每個列以返回彙總表。現在,對於分類在Answer Label
列下的任何Variable Name
,我想將Variable Type
和Answer Code
向下移一行。R:DataFrame格式化操作
示例代碼:
CreateCodebook <- function(dF){
numbercols <- length(colnames(dF))
table <- data.frame()
for (i in 1:length(colnames(dF))){
AnswerCode <- if (sapply(dF, is.factor)[i]) 1:nrow(unique(dF[i])) else NA
AnswerLabel <- if (sapply(dF, is.factor)[i]) as.vector(unique(dF[order(dF[i]),][i])) else "Open ended"
VariableName <- if (length(AnswerCode) > 1) c(colnames(dF)[i],
rep(NA,length(AnswerCode) - 1)) else colnames(dF)[i]
VariableLabel <- if (length(AnswerCode) > 1) c(colnames(dF)[i],
rep(NA,length(AnswerCode) - 1)) else colnames(dF)[i]
VariableType <- if (length(AnswerCode) > 1) c(sapply(dF, class)[i],
rep(NA,length(AnswerCode) - 1)) else sapply(dF, class)[i]
df = data.frame(VariableName, VariableLabel, AnswerLabel, AnswerCode, VariableType, stringsAsFactors = FALSE)
names(df) <- c("Variable Name", "Variable Label", "Variable Type", "Answer Code", "Answer Label")
table <- rbind(table, df)
}
rownames(table) <- 1:nrow(table)
return(table)
}
使用該數據集MASS::anorexia
,我得到這個輸出從我的功能:
Variable Name Variable Label Variable Type Answer Code Answer Label
1 Treat Treat CBT 1 factor
2 <NA> <NA> Cont 2 <NA>
3 <NA> <NA> FT 3 <NA>
4 Prewt Prewt Open ended NA numeric
5 Postwt Postwt Open ended NA numeric
所需的輸出:
Variable Name Variable Label Variable Type Answer Code Answer Label
1 Treat Treat <NA> NA factor
2 <NA> <NA> CBT 1 <NA>
3 <NA> <NA> Cont 2 <NA>
4 <NA> <NA> FT 3 <NA>
5 Prewt Prewt Open ended NA numeric
6 Postwt Postwt Open ended NA numeric
確保你提供[再現的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)與樣品輸入到測試函數。 – MrFlick
謝謝。我現在在我的文章中提供了一個可重現的例子。 –