[英]calculate and plot correlation coefficient in r
It may be an easy question but I am still a beginner in r. 这可能是一个简单的问题,但我仍然是r的初学者。
I need to calculate the correlation coefficient between each two numerical variables of the three columns in my dataframe and plot them. 我需要计算数据框中三列的每两个数字变量之间的相关系数,并将其绘制出来。
I want to have between columns 2 & 3, columns 2 & 4 and finally between columns 3 & 4. 我想在第2列和第3列之间,第2列和第4列之间,最后在第3列和第4列之间。
Thanks a lot in advance. 非常感谢。
You could use the following code: I recreated the first 3 rows of your data set and put them in a data frame called "mydata" 您可以使用以下代码:我重新创建了数据集的前3行,并将其放入名为“ mydata”的数据框中
cname <- c("Albania", "Argentina", "Australia")
economic_growth_rate <- c(75.67, 6.87, 24.22)
ave_HDI_rate <- c(8.69, 7.03, 3.61 )
ave_raw_EPI_growth_percentage <- c(16.61, -12.39, -1.77)
mydata <- data.frame(cname, economic_growth_rate, ave_HDI_rate, ave_raw_EPI_growth_percentage)
cor(mydata[ , 2:4])
This results in a correlation matrix. 这导致相关矩阵。
The last line in the above code selects column 2 up to 4 from the dataset mydata and passes it to the function cor. 上面的代码的最后一行从数据集mydata中选择第2列至第4列,并将其传递给函数cor。
you could render a barplot like this: 您可以这样绘制一个barplot:
cordf <- cor(mydata[ , 2:4])
barplot(cordf[,1])
type in the console for more info: 在控制台中键入以获取更多信息:
?cor
?barplot
or you could look at the packages corrgram and corrplot 或者您可以查看软件包corrgram和corrplot
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.