-6

When I use correlation function in RstudioThis I get below error message:

My error

The dataset that I have imported is show below and V1,V2 are given by default to the columns:

My dataset

ZF007
  • 3,708
  • 8
  • 29
  • 48
Sampath
  • 7
  • 2
  • 2
    And? What is your question? Please spend some time familiarising yourself with [how to ask](https://stackoverflow.com/help/how-to-ask) questions here on SO, and then provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including sample data (a screenshot is *not* useful). – Maurits Evers Feb 25 '18 at 11:49
  • I think you should write something like `data1$V1` – Alaleh Feb 25 '18 at 11:50
  • Your question belongs [here](https://stats.stackexchange.com/) at SO stats ("cross validated") sister site; flagged as such. – ZF007 Feb 25 '18 at 11:55
  • @ZF007 I don't think this is a stats question; this is more likely related to (a lack of understanding of) basic R syntax. But it's difficult to say without more details and sample data from OP. – Maurits Evers Feb 25 '18 at 12:28
  • @MauritsEvers... from your profile I see you're a better judge on this one. Then if so.. some of these type of q's should not be (auto)deleted and stay here for training purpose to newbie's who post these things. Three more down-votes..and its deleted.. followed by a new repetitive round of what we just did ;-( – ZF007 Feb 25 '18 at 12:33
  • @ZF007 I wouldn't say I'm a better judge than anyone else contributing here;-) SO provides excellent tutorials/howtos/advice regarding what to post; I'd say it's minimal effort for new SO members to familiarise themselves with those rules prior to posting. If they don't, there's not much we can do. I agree with you, this can be frustrating and repetitive. – Maurits Evers Feb 25 '18 at 12:46

2 Answers2

1

So, if I got you correctly, you want to find correlation between variables V1 and V2 in the dataframe data1. To refer to the column in the dataframe a $ sign is used. Then your code will look like:

cor(data1$V1,data1$V2)

or, if you want, you can also use with() function, which would narrow down the namespace to particular dataframe data1:

with(data1, cor(V1, V2))
1

There are two ways:

1) Attaching the data:

attach(data1)

And then this code should work:

cor(V1, V2)

2) Using $ for accesing columns in a dataframe

cor(data1$V1, data1$V2)
User 6683331
  • 692
  • 1
  • 13
  • 31