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
1
  • 1
    I'm unsure what you're trying to do here. You can check the correlation of 2 columns of your data frame in the data frame itself by specifying either the column names or the column indices to the cor() function. Commented Oct 17, 2013 at 10:06

1 Answer 1

6

If you want to create a matrix from your two-columns data frame, there is a more direct and simpler way : just transform you data frame as a matrix directly :

mat_corel <- as.matrix(tbl_corel)

But if you just want to compute a correlation coefficient, you can do it directly from your data frame :

cor(tbl_end$diff, tbl_end$abund_mean)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, that's so easy to do if you ask someone. That's what I wanted. I tried "mat_corel <- matrix(tbl_corel)". Forgot about "as". I wish I could vote up your answer. Thx once again.
Not asking for rep, but do you know that even if you can't upvote an answer, you can accept it by checking the green mark on its left ? This allows for visitors to know at a glance that the question had been answered.
Already did. Trying now to plot my results using: "pdf(file="myplot.pdf", width = 10, height =13) #create new pdf plot(diff ~ abund_mean) #see data but got this problem "Error in (function (formula, data = NULL, subset = NULL, na.action = na.fail, : object is not a matrix"
Use plot(diff~abund_mean, data=tbl_end). But please don't ask questions in a comment, Open a new question if you really ned it.
Okey, in future I will make another question. I was just worried about my rep.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.