本文介绍了如何从data.frame中的cor.test()中提取p.value和估计值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在这个例子中,我有来自50个不同网站的温度值,我想将Site1与所有50个网站关联起来。但是我只想提取使用函数生成的 p.value 和 估计 将data.frame中的strong> cor.test()划分为两个不同的列。 我已经完成了我的尝试,但是我不知道该怎么做! 出于这个原因,我想知道我怎么能简化我的代码,因为问题是,我必须运行两次循环for才能得到我的结果。 $ p $ b $ p $ #温度数据 data dimnames = list(c(paste(Year,1:100)),c (Site,1:50))))#清空data.frame df< - data.frame(label = paste(Site,1:50),Estimate = ,()()()()()()()()()() [,i]) df [,2:3]< - df1 [c(估计,p.value)] } 在1:50中){ df1 df [i,2:3] } df 我非常感谢你们的帮助: 首先,我猜你有一个错字在你的代码(你应该有 RNORM(5000 如果你想唯一值。否则,你会循环访问这500个数字10次。 无论如何,一个简单的方法是: $ b数据矩阵(rnorm(5000,10:30,sd = 5),nrow = 100,ncol = 50,byrow = TRUE, dimnames = list(c(paste(Year,1:100)),c(paste(Site,1:50))))#空数据框架 df< ; - data.frame(label = paste(Site,1:50),Estimate =,P.value =) estimated = numeric(50) pvalues = numeric(50 ) for(i in 1:50){ test< - cor.test(data [,1],data [,i]) estimated [i] = test $ estimate pvalues [i] = test $ p.value } df $估计值 df $ P.value df 编辑:我相信你的问题是在 df< - data .frame(label = paste(Site,1:50),Estimate =,P.value =)如果你做 typeof(df $ Estimate) ,你会发现它期望一个整数,而 ty peof(test $ estimate)表明它吐出了一个double,所以R不知道你想用这两个值做什么。你可以像这样重做你的代码: df for(i in 1:50){ test< - cor.test(data [,1], data [,i]) df $ Estimate [i] = test $ estimate df $ P.value [i] = test $ p.value } 使它更简洁一些。 In this example, I have temperatures values from 50 different sites, and I would like to correlate the Site1 with all the 50 sites. But I want to extract only the components "p.value" and "estimate" generated with the function cor.test() in a data.frame into two different columns. I have done my attempt and it works, but I don't know how!For that reason I would like to know how can I simplify my code, because the problem is that I have to run two times a Loop "for" to get my results.Here is my example:# Temperature data data <- matrix(rnorm(500, 10:30, sd=5), nrow = 100, ncol = 50, byrow = TRUE, dimnames = list(c(paste("Year", 1:100)), c(paste("Site", 1:50))) )# Empty data.frame df <- data.frame(label=paste("Site", 1:50), Estimate="", P.value="")# Extractionfor (i in 1:50) { df1 <- cor.test(data[,1], data[,i] ) df[,2:3] <- df1[c("estimate", "p.value")] }for (i in 1:50) { df1 <- cor.test(data[,1], data[,i] ) df[i,2:3] <- df1[c("estimate", "p.value")] } dfI will appreciate very much your help :) 解决方案 First of all, I'm guessing you had a typo in your code (you should have rnorm(5000 if you want unique values. Otherwise you're going to cycle through those 500 numbers 10 times.Anyway, a simple way of doing this would be:data <- matrix(rnorm(5000, 10:30, sd=5), nrow = 100, ncol = 50, byrow = TRUE, dimnames = list(c(paste("Year", 1:100)), c(paste("Site", 1:50))) )# Empty data.framedf <- data.frame(label=paste("Site", 1:50), Estimate="", P.value="")estimates = numeric(50)pvalues = numeric(50)for (i in 1:50){ test <- cor.test(data[,1], data[,i]) estimates[i] = test$estimate pvalues[i] = test$p.value}df$Estimate <- estimatesdf$P.value <- pvaluesdfEdit: I believe your issue was is that in the line df <- data.frame(label=paste("Site", 1:50), Estimate="", P.value="") if you do typeof(df$Estimate), you see it's expecting an integer, and typeof(test$estimate) shows it spits out a double, so R doesn't know what you're trying to do with those two values. you can redo your code like thus:df <- data.frame(label=paste("Site", 1:50), Estimate=numeric(50), P.value=numeric(50))for (i in 1:50){ test <- cor.test(data[,1], data[,i]) df$Estimate[i] = test$estimate df$P.value[i] = test$p.value}to make it a little more concise. 这篇关于如何从data.frame中的cor.test()中提取p.value和估计值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-22 07:06