Another R error: “comparison of these types is not implemented”
This works as a standalone statement, but errors when implemented in a function:
Given a data frame dummyDF with columns Status and LastStatusChangeDate, this works:
> nrow(dummyDF[which(dummyDF$Status == "Confirmed" & dummyDF$LastStatusChangeDate < as.Date("2018-11-30")),])
[1] 9
But when I implement it in a function:
QueueLength <- function(df, Status, LastDate){
nrow(df[which(df$Status == Status & df$LastStatusChangeDate < as.Date(LastDate)),])
}
and call it thus:
QueueLength(dummyDF, "Confirmed", "2018-11-30")
I get this error:
Error in df$Status == Status :
comparison of these types is not implemented
So what is the type difference? I ran this line:
> typeof(dummyDF$Status)
[1] "list"
OK, so I'm trying to compare a single string to a list of strings. I get it, sort of, why that is a problem, but it doesn't account for the body of the function working perfectly fine when I execute it interactively as above.
Anyone have a thought on how to make this work in a function?
Thanks.
r function dataframe
add a comment |
This works as a standalone statement, but errors when implemented in a function:
Given a data frame dummyDF with columns Status and LastStatusChangeDate, this works:
> nrow(dummyDF[which(dummyDF$Status == "Confirmed" & dummyDF$LastStatusChangeDate < as.Date("2018-11-30")),])
[1] 9
But when I implement it in a function:
QueueLength <- function(df, Status, LastDate){
nrow(df[which(df$Status == Status & df$LastStatusChangeDate < as.Date(LastDate)),])
}
and call it thus:
QueueLength(dummyDF, "Confirmed", "2018-11-30")
I get this error:
Error in df$Status == Status :
comparison of these types is not implemented
So what is the type difference? I ran this line:
> typeof(dummyDF$Status)
[1] "list"
OK, so I'm trying to compare a single string to a list of strings. I get it, sort of, why that is a problem, but it doesn't account for the body of the function working perfectly fine when I execute it interactively as above.
Anyone have a thought on how to make this work in a function?
Thanks.
r function dataframe
2
can you provide dummyDF so we can reproduce the error?
– iod
Nov 22 '18 at 0:58
1
Furher noodling around revealed that the "Status" column was produced by using lapply which created the values as a nested list. I wrapped the lapply() in unlist() and that cured THAT problem. One of these days I'll get my head around the apply() family. The error message has disappeared, but the function looks like it's only using the "LastStatusChangeDate" value in filtering. I'll work on it some more tomorrow and post again. Thanks.
– Charles Knell
Nov 22 '18 at 3:21
QueueLength <- function(df, Status, LastDate){ nrow(df[which(df$`Status` == Status & df$LastStatusChangeDate < as.Date(LastDate)),]) }
doesn't work too?
– Nutle
Nov 22 '18 at 15:24
I never worked out what the problem was, but when I created dummDF as a tibble rather than as a dataframe, my problems disappeared. Generally I like to stick with base R, but there are good reasons these other packages exist. Hats off to Tidyverse.
– Charles Knell
Nov 22 '18 at 20:15
add a comment |
This works as a standalone statement, but errors when implemented in a function:
Given a data frame dummyDF with columns Status and LastStatusChangeDate, this works:
> nrow(dummyDF[which(dummyDF$Status == "Confirmed" & dummyDF$LastStatusChangeDate < as.Date("2018-11-30")),])
[1] 9
But when I implement it in a function:
QueueLength <- function(df, Status, LastDate){
nrow(df[which(df$Status == Status & df$LastStatusChangeDate < as.Date(LastDate)),])
}
and call it thus:
QueueLength(dummyDF, "Confirmed", "2018-11-30")
I get this error:
Error in df$Status == Status :
comparison of these types is not implemented
So what is the type difference? I ran this line:
> typeof(dummyDF$Status)
[1] "list"
OK, so I'm trying to compare a single string to a list of strings. I get it, sort of, why that is a problem, but it doesn't account for the body of the function working perfectly fine when I execute it interactively as above.
Anyone have a thought on how to make this work in a function?
Thanks.
r function dataframe
This works as a standalone statement, but errors when implemented in a function:
Given a data frame dummyDF with columns Status and LastStatusChangeDate, this works:
> nrow(dummyDF[which(dummyDF$Status == "Confirmed" & dummyDF$LastStatusChangeDate < as.Date("2018-11-30")),])
[1] 9
But when I implement it in a function:
QueueLength <- function(df, Status, LastDate){
nrow(df[which(df$Status == Status & df$LastStatusChangeDate < as.Date(LastDate)),])
}
and call it thus:
QueueLength(dummyDF, "Confirmed", "2018-11-30")
I get this error:
Error in df$Status == Status :
comparison of these types is not implemented
So what is the type difference? I ran this line:
> typeof(dummyDF$Status)
[1] "list"
OK, so I'm trying to compare a single string to a list of strings. I get it, sort of, why that is a problem, but it doesn't account for the body of the function working perfectly fine when I execute it interactively as above.
Anyone have a thought on how to make this work in a function?
Thanks.
r function dataframe
r function dataframe
asked Nov 22 '18 at 0:54


Charles KnellCharles Knell
304
304
2
can you provide dummyDF so we can reproduce the error?
– iod
Nov 22 '18 at 0:58
1
Furher noodling around revealed that the "Status" column was produced by using lapply which created the values as a nested list. I wrapped the lapply() in unlist() and that cured THAT problem. One of these days I'll get my head around the apply() family. The error message has disappeared, but the function looks like it's only using the "LastStatusChangeDate" value in filtering. I'll work on it some more tomorrow and post again. Thanks.
– Charles Knell
Nov 22 '18 at 3:21
QueueLength <- function(df, Status, LastDate){ nrow(df[which(df$`Status` == Status & df$LastStatusChangeDate < as.Date(LastDate)),]) }
doesn't work too?
– Nutle
Nov 22 '18 at 15:24
I never worked out what the problem was, but when I created dummDF as a tibble rather than as a dataframe, my problems disappeared. Generally I like to stick with base R, but there are good reasons these other packages exist. Hats off to Tidyverse.
– Charles Knell
Nov 22 '18 at 20:15
add a comment |
2
can you provide dummyDF so we can reproduce the error?
– iod
Nov 22 '18 at 0:58
1
Furher noodling around revealed that the "Status" column was produced by using lapply which created the values as a nested list. I wrapped the lapply() in unlist() and that cured THAT problem. One of these days I'll get my head around the apply() family. The error message has disappeared, but the function looks like it's only using the "LastStatusChangeDate" value in filtering. I'll work on it some more tomorrow and post again. Thanks.
– Charles Knell
Nov 22 '18 at 3:21
QueueLength <- function(df, Status, LastDate){ nrow(df[which(df$`Status` == Status & df$LastStatusChangeDate < as.Date(LastDate)),]) }
doesn't work too?
– Nutle
Nov 22 '18 at 15:24
I never worked out what the problem was, but when I created dummDF as a tibble rather than as a dataframe, my problems disappeared. Generally I like to stick with base R, but there are good reasons these other packages exist. Hats off to Tidyverse.
– Charles Knell
Nov 22 '18 at 20:15
2
2
can you provide dummyDF so we can reproduce the error?
– iod
Nov 22 '18 at 0:58
can you provide dummyDF so we can reproduce the error?
– iod
Nov 22 '18 at 0:58
1
1
Furher noodling around revealed that the "Status" column was produced by using lapply which created the values as a nested list. I wrapped the lapply() in unlist() and that cured THAT problem. One of these days I'll get my head around the apply() family. The error message has disappeared, but the function looks like it's only using the "LastStatusChangeDate" value in filtering. I'll work on it some more tomorrow and post again. Thanks.
– Charles Knell
Nov 22 '18 at 3:21
Furher noodling around revealed that the "Status" column was produced by using lapply which created the values as a nested list. I wrapped the lapply() in unlist() and that cured THAT problem. One of these days I'll get my head around the apply() family. The error message has disappeared, but the function looks like it's only using the "LastStatusChangeDate" value in filtering. I'll work on it some more tomorrow and post again. Thanks.
– Charles Knell
Nov 22 '18 at 3:21
QueueLength <- function(df, Status, LastDate){ nrow(df[which(df$`Status` == Status & df$LastStatusChangeDate < as.Date(LastDate)),]) }
doesn't work too?– Nutle
Nov 22 '18 at 15:24
QueueLength <- function(df, Status, LastDate){ nrow(df[which(df$`Status` == Status & df$LastStatusChangeDate < as.Date(LastDate)),]) }
doesn't work too?– Nutle
Nov 22 '18 at 15:24
I never worked out what the problem was, but when I created dummDF as a tibble rather than as a dataframe, my problems disappeared. Generally I like to stick with base R, but there are good reasons these other packages exist. Hats off to Tidyverse.
– Charles Knell
Nov 22 '18 at 20:15
I never worked out what the problem was, but when I created dummDF as a tibble rather than as a dataframe, my problems disappeared. Generally I like to stick with base R, but there are good reasons these other packages exist. Hats off to Tidyverse.
– Charles Knell
Nov 22 '18 at 20:15
add a comment |
0
active
oldest
votes
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%2f53422487%2fanother-r-error-comparison-of-these-types-is-not-implemented%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53422487%2fanother-r-error-comparison-of-these-types-is-not-implemented%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
2
can you provide dummyDF so we can reproduce the error?
– iod
Nov 22 '18 at 0:58
1
Furher noodling around revealed that the "Status" column was produced by using lapply which created the values as a nested list. I wrapped the lapply() in unlist() and that cured THAT problem. One of these days I'll get my head around the apply() family. The error message has disappeared, but the function looks like it's only using the "LastStatusChangeDate" value in filtering. I'll work on it some more tomorrow and post again. Thanks.
– Charles Knell
Nov 22 '18 at 3:21
QueueLength <- function(df, Status, LastDate){ nrow(df[which(df$`Status` == Status & df$LastStatusChangeDate < as.Date(LastDate)),]) }
doesn't work too?– Nutle
Nov 22 '18 at 15:24
I never worked out what the problem was, but when I created dummDF as a tibble rather than as a dataframe, my problems disappeared. Generally I like to stick with base R, but there are good reasons these other packages exist. Hats off to Tidyverse.
– Charles Knell
Nov 22 '18 at 20:15