Calculate correlation for more than two variables?

前端 未结 5 1660
眼角桃花
眼角桃花 2021-01-30 02:36

I use the following method to calculate a correlation of my dataset:

cor( var1, var2, method = \"method\")

But I like to create a correlation m

5条回答
  •  后悔当初
    2021-01-30 03:17

    You can also calculate correlations for all variables but exclude selected ones, for example:

    mtcars <- data.frame(mtcars)
    # here we exclude gear and carb variables
    cors <- cor(subset(mtcars, select = c(-gear,-carb)))
    

    Also, to calculate correlation between each variable and one column you can use sapply()

    # sapply effectively calls the corelation function for each column of mtcars and mtcars$mpg
    cors2 <- sapply(mtcars, cor, y=mtcars$mpg)
    

提交回复
热议问题