I am new to R and am trying to run the following loop:
CorrData <- read.csv("F:/CorrelationDataSets)
HIVNYears2=list()
par( mfrow=c(3,3))
for (i in unique(CorrData$Year))
{ x=as.numeric(CorrData[CorrData$Year==i,]$NumVisits)
y=as.numeric(CorrData[CorrData$Year==i,]$HIVN )
plot(x , y , col="red" , type="p" , pch=16 , main=i )
HIVNYears2[[i]]<-cor.test(x, y, method="spearman",na.action=na.exclude, by=CorrData$Year)
}
HIVNYears2
I have 93 counties and data for all of them for the past 7 years. In this loop, I am trying to run correlations by year--specifically for the number of HIV cases and the number of visits to the healthcare facility.
When I run this code, it gives me 2016 results, when I am only wanting/expecting 7--one Spearman Correlation for each of the seven years that data was collected between the two variables. The plots look as expected, but the results of the cor.test() do not.
Would you be able to help identify the error in the loop?
Thank you in advance!
答案 0 :(得分:3)
我认为它不会给你2016年的结果,我认为它将结果放在2010 - 2017年的输出矢量指数中。也就是说,
let hardCost = candyArray.costOf(consistency: "hard")
let softCost = candyArray.costOf(consistency: "soft")
let chewyCost = candyArray.costOf(consistency: "chewy")
将结果显示为2010年的单位,并将[无论如何]放在2010年的位置。
有许多方法可以改进您的代码,但只是为了解决这个问题,使Year成为一个字符向量而不是数字。
HIVNYears2[[2010]] <- [whatever]
改进代码的最“基础R”方法是拆分数据集,然后在每个部分上运行代码。
CorrData$Year <- as.character(CorrData$Year)
进一步改进可能会使用CorrDataSplit <- split(CorrData, CorrData$Year)
for(subdata in CorrDataSplit) { [run code] }
,这通常是首选而不是for循环,或新的lapply
包,它们有很多选项可以像这样做。