简体   繁体   English

在 R 中,如何根据来自其他两列的输入使用 CIr 函数的结果填充两列?

[英]In R, how can I populate two columns with the results of the CIr function based on inputs from two other columns?

Thanks for any help in advance.提前感谢您的任何帮助。 I have a dataset with correlation values in a column called 'exit' and corresponding sample sizes (n) in a column called 'samplesize' in a data frame called 'dataset'.我有一个数据集,在名为“exit”的列中具有相关值,在名为“dataset”的数据框中名为“samplesize”的列中具有相应的样本大小(n)。

My task is to create an R script to populate two full columns (CIleft and CIright) with the confidence interval outputs using the CIr function within the "psychometric" package for each row of data.我的任务是创建一个 R 脚本,使用“心理测量”包中的 CIr 函数为每行数据填充带有置信区间输出的两个完整列(CIleft 和 CIright)。 This CIr function operates as follows, outputting the left and right confidence interval values:此 CIr 函数操作如下,输出左右置信区间值:

CIr(r = .9, n = 100, level = .95)  
[1] 0.8546667 0.9317133

Below is my unsuccessful script.下面是我失败的脚本。

CI <- function(x)
{
  require(psychometric)
  library(psychometric)
  r <- x["dataset$exit"];
  n <- x["dataset$samplesize"];
  results <- CIr(r, n, level = .95);
  x["dataset$CIleft"] <- results[1];
  x["dataset$CIright"] <- results[2];
}

One complication (which I believe may be relevant) is that test runs of "CI(x)" in the console produce the following errors:一种复杂情况(我认为可能相关)是控制台中“CI(x)”的测试运行会产生以下错误:

// Error in CIz(z, n, level) : (list) object cannot be coerced to type 'double' 

Then entering dataset2 <- as.matrix(dataset) and trying CI(x) again yields:然后输入dataset2 <- as.matrix(dataset)并再次尝试CI(x)产生:

Error in dataset2$exit : $ operator is invalid for atomic vectors 

And for而对于

dataset3 <- lapply(dataset$exit, as.numeric)
dataset4 <- lapply(dataset$samplesize, as.numeric)

trying CI(x) again yields:再次尝试CI(x)产生:

Error in 1 + x : non-numeric argument to binary operator //

Can anyone assist in helping me populate each row of my data frame with the appropriate output for CIleft and CIright , given that r = 'exit' , and n = 'samplesize' ?鉴于r = 'exit'n = 'samplesize' CIright r = 'exit' ,任何人都可以帮助我用CIleftCIright的适当输出填充数据框的每一行吗?

I don't think you need a function.我不认为你需要一个功能。

library("psychometric")

dataset$lwr = NULL
dataset$upr = NULL

for (row in 1:nrow(dataset)){
   dataset[["lwr"]][row] <- CIr(r = dataset[["exit"]][row], n = dataset[["samplesize"]][row], level = .95)[1]
   dataset[["upr"]][row] <- CIr(r = dataset[["exit"]][row], n = dataset[["samplesize"]][row], level = .95)[2]
}

I will note though that it's generally advisable to avoid for loops in R because of its architecture (ie, they're slow).我会注意到,通常建议避免R for循环,因为它的体系结构(即它们很慢)。 Perhaps someone else can provide a solution with something else, eg, apply .也许其他人可以提供其他解决方案,例如apply However, if you only have a small dataframe, the speed cost of using a for loop is unlikely to be noticeable.但是,如果您只有一个小数据帧,则使用for循环的速度成本不太可能引起注意。


Test Data:测试数据:

set.seed(55); m = rnorm(26, 20, 40); dataset = data.frame( exit = seq(0, 1, 0.04), samplesize = abs(round(m)))
dataset$samplesize[dataset$samplesize == 0] = 5
dataset$exit[dataset$exit == 1] = 0.99

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM