简体   繁体   English

如何将 cat 结果保存为 data.frame

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

Here Im trying to save my results as data.frame but I couldn't the only way I was able to show them by using "cat"在这里,我试图将我的结果保存为 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")
  }
}

Is anyone able to save the results as data.frame?有人可以将结果保存为data.frame吗?

It is a bit more involved since you are doing multiple analyses within each group.由于您要在每个组中进行多项分析,因此涉及更多一些。 Modify your code as follows.如下修改您的代码。 First, insert the following before entering the first loop:首先,在进入第一个循环之前插入以下内容:

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

Now replace the cat statement with the following:现在将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)

Now you can print the data frame:现在您可以打印数据框:

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