0

I am so desperated and even I am ready to lose some more rep points but I have to ask it. (Yes, I read some threads about it).

I created a dataframe with only 2 columns I want to put to the matrix (I didn't know how to pick just 2 columns from whole data):

    tbl_corel <- tbl_end[,c("diff", "abund_mean")]

In next step I created and empty matrix:

## Creating a empty matrix to check the correlation between diff and abund_mean
mat_corel <- matrix(0, ncol = 2)
colnames(mat_corel) <- c("diff", "abund_mean")

I tried to use that function to fill the matrix with the data:

mat_corel <- matrix(tbl_corel), nrow = 676,ncol = 2)

Of course I had to check manually how many rows I have in my data frame... It doesn't work. Tried that function as well:

mat_corel[ as.matrix(tbl_corel) ] <- 1

It doesn't work. I'd be so grateful for the help.

  diff abund_mean
1    0 3444804.80
2    0  847887.02
3    0   93654.19
4    0  721692.76
5    0  382711.04
6    1  428656.66
4

1 回答 1

5

如果你想从你的两列数据框创建一个矩阵,有一种更直接和更简单的方法:只需将你的数据框直接转换为矩阵:

mat_corel <- as.matrix(tbl_corel)

但是,如果您只想计算相关系数,则可以直接从数据框中进行:

cor(tbl_end$diff, tbl_end$abund_mean)
于 2013-10-17T10:07:28.383 回答