簡體   English   中英

如何將 cat 結果保存為 data.frame

[英]How to save cat results as data.frame

在這里,我試圖將我的結果保存為 data.frame 但我無法使用“cat”來顯示它們的唯一方法

library(metafor)
id <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3)
year <- c(1978, 1983, 1974, 1989, 1974, 2002, 1990, 1974, 1998, 1989, 1974, 1983, 1978, 1998, 1978, 1974, 1974, 1998)
study.name <- c("Banninger 1978", "Roberts 1983", "Beard 1974", "Mahomed 1989", "Livingstone 1974", "Upton 2002", "Olah 1990", "Rogers 1974", "Mackrodt 1998",
                "Mahomed 1989", "Beard 1974", "Roberts 1983", "Banninger 1978", "Mackrodt 1998", "Banninger 1978", "Beard 1974", "Livingstone 1974", "Mackrodt 1998")
y <- c(-0.81, -0.87, -0.67, -0.77, -0.03, -0.94, -0.78, -0.12, -0.34, -0.34, -0.76, -0.87, -0.55, -0.99, -0.44, -0.14, -0.34, -0.76)
s <- c(0.11, 0.19, 0.05, 0.17, 0.09, 0.03, 0.08, 0.22, 0.21, 0.27, 0.15, 0.04, 0.15, 0.09, 0.02, 0.11, 0.03, 0.09)
data <- as.data.frame(cbind(id, year, study.name, y, s))

data.ids <- unique(data$id)
n.ma.binary <- length(data.ids)
for(i in 1:n.ma.binary){
  temp <- data[data$id == data.ids[i],]
  temp <- temp[order(temp$year),]                    # sorting MA by year
  
  list <- lapply(4:nrow(temp)-1, function(k) head(temp, k))
  
  
  for(j in 1:length(list)){
    dd <- as.data.frame(list[j])
    
    result <- rma(yi = as.numeric(dd$y), vi = as.numeric(dd$s))
    alpha <- 0.05
    n <- result$k       
    # To get PI
    lower.PI <- result$b - qt(1-alpha/2,n-2)*sqrt(2*result$tau2 + (result$se.tau2)^2)
    upper.PI <- result$b + qt(1-alpha/2,n-2)*sqrt(2*result$tau2 + (result$se.tau2)^2)
  
    remaining <- temp[ !(temp$study.name %in% dd$study.name), ]
    
    decision <- as.numeric(ifelse(sapply(remaining$y, function(p) 
      any(lower.PI <= p & upper.PI >= p)),"1", "0"))
    
    proportion <- mean(decision)

    cat(list.num = j, new.id = unique(temp$id), proportion = proportion, 
        lower.PI = lower.PI, upper.PI = upper.PI,  "\n")
  }
}

有人可以將結果保存為data.frame嗎?

由於您要在每個組中進行多項分析,因此涉及更多一些。 如下修改您的代碼。 首先,在進入第一個循環之前插入以下內容:

results.all <- data.frame(list.num=NA, new.id=NA, proportion=NA, lower.PI=NA, upper.PI=NA)
idx <- 0

現在將cat語句替換為以下內容:

idx <- idx+1
results.all[idx,] <- c(list.num = j, new.id = unique(temp$id), proportion = proportion, lower.PI = lower.PI, upper.PI = upper.PI)

現在您可以打印數據框:

results.all
#   list.num new.id proportion           lower.PI           upper.PI
# 1        1      1          0  -5.75317346362513   5.07162070815196
# 2        2      1          0  -1.97747383048426   1.06346158846251
# 3        3      1          0  -1.44463494970073  0.414992978516971
# 4        4      1          0  -1.14935843511063 0.0548216056175898
# 5        5      1          0 -0.886747285977298 -0.295909268008411
# 6        6      1          0 -0.710099787395835 -0.441424896621657
# 7        1      2          0  -2.02192058321991  0.431485800611215
# 8        2      2          0  -1.13751587707834 -0.372704387238843
# 9        1      3          0  -0.80174083281866 0.0528883738022665

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM