I want to use the cor() function but output says 'y' must be numeric. Problem is, it is numeric
I've reading data from an xlsx file. My read code start off like this:
ecommerce<-read.xlsx("C:\Users\Thomas Rhee\Documents\GGU\GGU Fall 2018\Tools for Business Analytics\Final Project\ecommerce.xlsx", sheet = "data", startRow = 1, colNames = TRUE, col = c(1,2,3,4,5,6,7,8));
attach(ecommerce)
names(ecommerce)
One of the columns is "price". It looks like this:
price
<chr>
329.98
324.83999999999997
324.83
350
308
310
I used the sapply
to find out my 'price' column's class is character. I use the following code to convert it into numeric:
ecommerce$price <- as.numeric(as.character(ecommerce$price))
I checked again and it worked. I tried typing the following and get this output:
cor(rank, price)
Error in cor(rank, price) : 'y' must be numeric
I'm lost. I'm also a beginner at this, so I'm open to suggestions here. Please dumb it down for me.
r correlation numeric
add a comment |
I've reading data from an xlsx file. My read code start off like this:
ecommerce<-read.xlsx("C:\Users\Thomas Rhee\Documents\GGU\GGU Fall 2018\Tools for Business Analytics\Final Project\ecommerce.xlsx", sheet = "data", startRow = 1, colNames = TRUE, col = c(1,2,3,4,5,6,7,8));
attach(ecommerce)
names(ecommerce)
One of the columns is "price". It looks like this:
price
<chr>
329.98
324.83999999999997
324.83
350
308
310
I used the sapply
to find out my 'price' column's class is character. I use the following code to convert it into numeric:
ecommerce$price <- as.numeric(as.character(ecommerce$price))
I checked again and it worked. I tried typing the following and get this output:
cor(rank, price)
Error in cor(rank, price) : 'y' must be numeric
I'm lost. I'm also a beginner at this, so I'm open to suggestions here. Please dumb it down for me.
r correlation numeric
add a comment |
I've reading data from an xlsx file. My read code start off like this:
ecommerce<-read.xlsx("C:\Users\Thomas Rhee\Documents\GGU\GGU Fall 2018\Tools for Business Analytics\Final Project\ecommerce.xlsx", sheet = "data", startRow = 1, colNames = TRUE, col = c(1,2,3,4,5,6,7,8));
attach(ecommerce)
names(ecommerce)
One of the columns is "price". It looks like this:
price
<chr>
329.98
324.83999999999997
324.83
350
308
310
I used the sapply
to find out my 'price' column's class is character. I use the following code to convert it into numeric:
ecommerce$price <- as.numeric(as.character(ecommerce$price))
I checked again and it worked. I tried typing the following and get this output:
cor(rank, price)
Error in cor(rank, price) : 'y' must be numeric
I'm lost. I'm also a beginner at this, so I'm open to suggestions here. Please dumb it down for me.
r correlation numeric
I've reading data from an xlsx file. My read code start off like this:
ecommerce<-read.xlsx("C:\Users\Thomas Rhee\Documents\GGU\GGU Fall 2018\Tools for Business Analytics\Final Project\ecommerce.xlsx", sheet = "data", startRow = 1, colNames = TRUE, col = c(1,2,3,4,5,6,7,8));
attach(ecommerce)
names(ecommerce)
One of the columns is "price". It looks like this:
price
<chr>
329.98
324.83999999999997
324.83
350
308
310
I used the sapply
to find out my 'price' column's class is character. I use the following code to convert it into numeric:
ecommerce$price <- as.numeric(as.character(ecommerce$price))
I checked again and it worked. I tried typing the following and get this output:
cor(rank, price)
Error in cor(rank, price) : 'y' must be numeric
I'm lost. I'm also a beginner at this, so I'm open to suggestions here. Please dumb it down for me.
r correlation numeric
r correlation numeric
edited Nov 28 '18 at 8:20
markus
14.4k11336
14.4k11336
asked Nov 28 '18 at 7:37
Tom R.Tom R.
61
61
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This is a good example why you should not use attach
.
d <- data.frame(x = 1:3)
attach(d)
x ## now available because of attach
# [1] 1 2 3
d$x <- LETTERS[1:3]
x ## however this still refers to the original values of d$x
# [1] 1 2 3
d$x
# [1] "A" "B" "C"
That means, you changed your original data in the data frame, but in your cor(.)
call you reference the original one (the one which was attached)
So to solve your issue, drop the attach
command and specify the columns directly (after you have transformed them to a numeric):
cor(ecommerce$rank, ecommerce$price)
Technically, you could re-attach ecommerce
again, after you changed it, but because of these issues I would strongly dis-encourage you to use attach
at all.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53514387%2fi-want-to-use-the-cor-function-but-output-says-y-must-be-numeric-problem-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a good example why you should not use attach
.
d <- data.frame(x = 1:3)
attach(d)
x ## now available because of attach
# [1] 1 2 3
d$x <- LETTERS[1:3]
x ## however this still refers to the original values of d$x
# [1] 1 2 3
d$x
# [1] "A" "B" "C"
That means, you changed your original data in the data frame, but in your cor(.)
call you reference the original one (the one which was attached)
So to solve your issue, drop the attach
command and specify the columns directly (after you have transformed them to a numeric):
cor(ecommerce$rank, ecommerce$price)
Technically, you could re-attach ecommerce
again, after you changed it, but because of these issues I would strongly dis-encourage you to use attach
at all.
add a comment |
This is a good example why you should not use attach
.
d <- data.frame(x = 1:3)
attach(d)
x ## now available because of attach
# [1] 1 2 3
d$x <- LETTERS[1:3]
x ## however this still refers to the original values of d$x
# [1] 1 2 3
d$x
# [1] "A" "B" "C"
That means, you changed your original data in the data frame, but in your cor(.)
call you reference the original one (the one which was attached)
So to solve your issue, drop the attach
command and specify the columns directly (after you have transformed them to a numeric):
cor(ecommerce$rank, ecommerce$price)
Technically, you could re-attach ecommerce
again, after you changed it, but because of these issues I would strongly dis-encourage you to use attach
at all.
add a comment |
This is a good example why you should not use attach
.
d <- data.frame(x = 1:3)
attach(d)
x ## now available because of attach
# [1] 1 2 3
d$x <- LETTERS[1:3]
x ## however this still refers to the original values of d$x
# [1] 1 2 3
d$x
# [1] "A" "B" "C"
That means, you changed your original data in the data frame, but in your cor(.)
call you reference the original one (the one which was attached)
So to solve your issue, drop the attach
command and specify the columns directly (after you have transformed them to a numeric):
cor(ecommerce$rank, ecommerce$price)
Technically, you could re-attach ecommerce
again, after you changed it, but because of these issues I would strongly dis-encourage you to use attach
at all.
This is a good example why you should not use attach
.
d <- data.frame(x = 1:3)
attach(d)
x ## now available because of attach
# [1] 1 2 3
d$x <- LETTERS[1:3]
x ## however this still refers to the original values of d$x
# [1] 1 2 3
d$x
# [1] "A" "B" "C"
That means, you changed your original data in the data frame, but in your cor(.)
call you reference the original one (the one which was attached)
So to solve your issue, drop the attach
command and specify the columns directly (after you have transformed them to a numeric):
cor(ecommerce$rank, ecommerce$price)
Technically, you could re-attach ecommerce
again, after you changed it, but because of these issues I would strongly dis-encourage you to use attach
at all.
edited Nov 28 '18 at 8:36
answered Nov 28 '18 at 8:28
thothalthothal
4,4491237
4,4491237
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53514387%2fi-want-to-use-the-cor-function-but-output-says-y-must-be-numeric-problem-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown