我当前正在创建一个空数据框,然后附加一组的每一行值。在最初的列之后会添加几十列,我想知道是否可以从列表中创建一些列名称:
我知道我可以像这样创建一个空数据框:
prssa.predictions <- data.frame("time"=integer(), "diagnosis"=character(),
"n_subj"=integer(), "acknowledge"=double(), "response.elab"=double(),
stringsAsFactors = FALSE)
但如果我有一个包含最后两列的列表(它们都是double
s),我可以在数据框创建中使用它吗?
prediction.list <- c("acknowledge", "response.elab")
为了完整性&#39;这是我对数据框的处理方式:
diagnosis.list <- c("AD_m", "DS_m", "FXa_m", "FXo_m", "TD_m")
# loop through all scores and append to prediction df
for (time in c(1,3)) {
for (diagnosis in diagnosis.list) {
# subset by time and diagnosis group
diagnosis.time.subset <- subset(prssa.p1p3,
ptime==time & group_ASDstatus==diagnosis)
subj.count <- nrow(diagnosis.time.subset)
ack.pred <- polr.t.score(diagnosis.time.subset, "acknowledge")
# append group info and scores to next row of df
prssa.predictions[nrow(prssa.predictions)+1, ] <- c(time, diagnosis, subj.count,
ack.pred)
答案 0 :(得分:1)
为什么不用for循环添加它们?
for (x in prediction.list)
prssa.predictions[[x]] <- double()