“subscript out of bounds error” when trying to knit to .md file? How to fix?How to fix “Subscript out of bounds error”Cryptic dplyr error when trying to knit htmlInexplicable “subscript out of bounds” error in dplyr::filter()Subscript out of bounds errorError in R: subscript out of boundsSubscript out of bounds when using [[]]Subscript out of bounds error in rmarkdownOut of bounds subscript errorDplyr - Mutate subscript out of boundsError knitting file in RStudio
What's the difference between 反面 and 一方?
What is the shape of the upper boundary of water hitting a screen?
What is the highest level of accuracy in motion control a Victorian society could achieve?
Why do most airliners have underwing engines, while business jets have rear-mounted engines?
How can I effectively map a multi-level dungeon?
LTSpice: how to setup sinusoidal or exponential voltage source?
How do I iterate equal values with the standard library?
Was I wrongfully denied boarding for having a Schengen visa issued from the second country on my itinerary?
How do I check that users don't write down their passwords?
Find max number you can create from an array of numbers
How would a sea turtle end up on its back?
How serious is plagiarism in a master’s thesis?
What happens if the limit of 4 billion files was exceeded in an ext4 partition?
Will Jimmy fall off his platform?
Did William Shakespeare hide things in his writings?
Will electrically joined dipoles of different lengths, at right angles, behave as a multiband antenna?
Machine Learning Golf: Multiplication
Why would "dead languages" be the only languages that spells could be written in?
Does 5e have an equivalent of the Psychic Paper from Doctor Who?
How did Captain Marvel do this without dying?
Is it acceptable that I plot a time-series figure with years increasing from right to left?
Should I cheat if the majority does it?
Sleepy tired vs physically tired
Boss furious on bad appraisal
“subscript out of bounds error” when trying to knit to .md file? How to fix?
How to fix “Subscript out of bounds error”Cryptic dplyr error when trying to knit htmlInexplicable “subscript out of bounds” error in dplyr::filter()Subscript out of bounds errorError in R: subscript out of boundsSubscript out of bounds when using [[]]Subscript out of bounds error in rmarkdownOut of bounds subscript errorDplyr - Mutate subscript out of boundsError knitting file in RStudio
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
my rmarkdown file seems to have an issue knitting on this specific chunk of code:
library(tidyverse)
df <- data.frame(female = c("Republican", "Republican", "Democrat", "Democrat"),
male = c("Republican", "Democrat", "Republican", "Democrat"),
n = c(100, 50, 50, 150))
xtabs(n ~ female + male, df)
# male
# female Democrat Republican
# Democrat 150 50
# Republican 50 100
I am trying to calculate the probability of a Rep Female being with a Rep Man, and vice versa. So in this example that would be (100 / 150), but I don't know how to do that in a relation to the variables.
I tried this:
Fem <- colSums(df[ , "Republican", drop = FALSE] ) / sum(colSums(df))
Fem
Men <- rowSums(df["Republican", , drop = FALSE ] ) / sum(rowSums(df))
Men
This works fine in the code and produces probabilities, but refuses to compile when I try to knit it into an md file. It says:
Error in `[.default`(df, , "Republican", drop = FALSE) : subscript out of bounds
Calls: <Anonymous> ... colSums -> -> is.data.frame -> [ -> [.table -> NextMethod
In addition: Warning messages:
1: package 'tidyverse' was built under R version 3.5.2
2: package 'tidyr' was built under R version 3.5.2
3: package 'forcats' was built under R version 3.5.2
Execution Halted
Could someone help me decipher where I messed up? Thank you!
r dplyr r-markdown
|
show 2 more comments
my rmarkdown file seems to have an issue knitting on this specific chunk of code:
library(tidyverse)
df <- data.frame(female = c("Republican", "Republican", "Democrat", "Democrat"),
male = c("Republican", "Democrat", "Republican", "Democrat"),
n = c(100, 50, 50, 150))
xtabs(n ~ female + male, df)
# male
# female Democrat Republican
# Democrat 150 50
# Republican 50 100
I am trying to calculate the probability of a Rep Female being with a Rep Man, and vice versa. So in this example that would be (100 / 150), but I don't know how to do that in a relation to the variables.
I tried this:
Fem <- colSums(df[ , "Republican", drop = FALSE] ) / sum(colSums(df))
Fem
Men <- rowSums(df["Republican", , drop = FALSE ] ) / sum(rowSums(df))
Men
This works fine in the code and produces probabilities, but refuses to compile when I try to knit it into an md file. It says:
Error in `[.default`(df, , "Republican", drop = FALSE) : subscript out of bounds
Calls: <Anonymous> ... colSums -> -> is.data.frame -> [ -> [.table -> NextMethod
In addition: Warning messages:
1: package 'tidyverse' was built under R version 3.5.2
2: package 'tidyr' was built under R version 3.5.2
3: package 'forcats' was built under R version 3.5.2
Execution Halted
Could someone help me decipher where I messed up? Thank you!
r dplyr r-markdown
df["Republican",]
only works if there are row names, your sample data does not. Do you meandf[ df$female == "Republican",,drop=FALSE]
? (Does that code work on the console, regardless of rmarkdown? I suggest when making your rmarkdown documents, you try things on the console before trying to automate and render your report.)
– r2evans
Mar 25 at 19:38
The code I provided works well for me on the console :/ It automatically generated row names for me. Your solution gives me this error: "Error in df$female : $ operator is invalid for atomic vectors"
– skipndipp
Mar 25 at 19:41
There must be code that you are not sharing, since that error occurs whendf
is not a frame. Are you by chance overwriting withdf <- xtabs(...)
? That's rather important. More the point,data.frame
does not auto-generate row names like that, and that's the only place you are assigning todf
.)
– r2evans
Mar 25 at 19:43
1
I promise that that's all the code I've used. And sorry about tidyverse, I only put it there since I know it's loaded and didn't know if it would affect the issue.
– skipndipp
Mar 25 at 19:48
1
Something is missing, of that I am confident. If you start a new R session (don't reload a cache! make sure the environment is empty withls()
), then type in your code as you present it here, you will get errors or warnings different from your question. Your assertion only makes sense (so far) if you dodf<-data.frame(...)
and thendf<-xtabs(...,data=df)
. Further, if it is the table and not the frame,df["Republican",,drop=F]
gets you the republicans, not the males (or females), so your summary statistic is mis-informing.
– r2evans
Mar 25 at 19:53
|
show 2 more comments
my rmarkdown file seems to have an issue knitting on this specific chunk of code:
library(tidyverse)
df <- data.frame(female = c("Republican", "Republican", "Democrat", "Democrat"),
male = c("Republican", "Democrat", "Republican", "Democrat"),
n = c(100, 50, 50, 150))
xtabs(n ~ female + male, df)
# male
# female Democrat Republican
# Democrat 150 50
# Republican 50 100
I am trying to calculate the probability of a Rep Female being with a Rep Man, and vice versa. So in this example that would be (100 / 150), but I don't know how to do that in a relation to the variables.
I tried this:
Fem <- colSums(df[ , "Republican", drop = FALSE] ) / sum(colSums(df))
Fem
Men <- rowSums(df["Republican", , drop = FALSE ] ) / sum(rowSums(df))
Men
This works fine in the code and produces probabilities, but refuses to compile when I try to knit it into an md file. It says:
Error in `[.default`(df, , "Republican", drop = FALSE) : subscript out of bounds
Calls: <Anonymous> ... colSums -> -> is.data.frame -> [ -> [.table -> NextMethod
In addition: Warning messages:
1: package 'tidyverse' was built under R version 3.5.2
2: package 'tidyr' was built under R version 3.5.2
3: package 'forcats' was built under R version 3.5.2
Execution Halted
Could someone help me decipher where I messed up? Thank you!
r dplyr r-markdown
my rmarkdown file seems to have an issue knitting on this specific chunk of code:
library(tidyverse)
df <- data.frame(female = c("Republican", "Republican", "Democrat", "Democrat"),
male = c("Republican", "Democrat", "Republican", "Democrat"),
n = c(100, 50, 50, 150))
xtabs(n ~ female + male, df)
# male
# female Democrat Republican
# Democrat 150 50
# Republican 50 100
I am trying to calculate the probability of a Rep Female being with a Rep Man, and vice versa. So in this example that would be (100 / 150), but I don't know how to do that in a relation to the variables.
I tried this:
Fem <- colSums(df[ , "Republican", drop = FALSE] ) / sum(colSums(df))
Fem
Men <- rowSums(df["Republican", , drop = FALSE ] ) / sum(rowSums(df))
Men
This works fine in the code and produces probabilities, but refuses to compile when I try to knit it into an md file. It says:
Error in `[.default`(df, , "Republican", drop = FALSE) : subscript out of bounds
Calls: <Anonymous> ... colSums -> -> is.data.frame -> [ -> [.table -> NextMethod
In addition: Warning messages:
1: package 'tidyverse' was built under R version 3.5.2
2: package 'tidyr' was built under R version 3.5.2
3: package 'forcats' was built under R version 3.5.2
Execution Halted
Could someone help me decipher where I messed up? Thank you!
r dplyr r-markdown
r dplyr r-markdown
edited Mar 25 at 19:44
r2evans
30.5k3 gold badges32 silver badges59 bronze badges
30.5k3 gold badges32 silver badges59 bronze badges
asked Mar 25 at 19:31
skipndippskipndipp
85 bronze badges
85 bronze badges
df["Republican",]
only works if there are row names, your sample data does not. Do you meandf[ df$female == "Republican",,drop=FALSE]
? (Does that code work on the console, regardless of rmarkdown? I suggest when making your rmarkdown documents, you try things on the console before trying to automate and render your report.)
– r2evans
Mar 25 at 19:38
The code I provided works well for me on the console :/ It automatically generated row names for me. Your solution gives me this error: "Error in df$female : $ operator is invalid for atomic vectors"
– skipndipp
Mar 25 at 19:41
There must be code that you are not sharing, since that error occurs whendf
is not a frame. Are you by chance overwriting withdf <- xtabs(...)
? That's rather important. More the point,data.frame
does not auto-generate row names like that, and that's the only place you are assigning todf
.)
– r2evans
Mar 25 at 19:43
1
I promise that that's all the code I've used. And sorry about tidyverse, I only put it there since I know it's loaded and didn't know if it would affect the issue.
– skipndipp
Mar 25 at 19:48
1
Something is missing, of that I am confident. If you start a new R session (don't reload a cache! make sure the environment is empty withls()
), then type in your code as you present it here, you will get errors or warnings different from your question. Your assertion only makes sense (so far) if you dodf<-data.frame(...)
and thendf<-xtabs(...,data=df)
. Further, if it is the table and not the frame,df["Republican",,drop=F]
gets you the republicans, not the males (or females), so your summary statistic is mis-informing.
– r2evans
Mar 25 at 19:53
|
show 2 more comments
df["Republican",]
only works if there are row names, your sample data does not. Do you meandf[ df$female == "Republican",,drop=FALSE]
? (Does that code work on the console, regardless of rmarkdown? I suggest when making your rmarkdown documents, you try things on the console before trying to automate and render your report.)
– r2evans
Mar 25 at 19:38
The code I provided works well for me on the console :/ It automatically generated row names for me. Your solution gives me this error: "Error in df$female : $ operator is invalid for atomic vectors"
– skipndipp
Mar 25 at 19:41
There must be code that you are not sharing, since that error occurs whendf
is not a frame. Are you by chance overwriting withdf <- xtabs(...)
? That's rather important. More the point,data.frame
does not auto-generate row names like that, and that's the only place you are assigning todf
.)
– r2evans
Mar 25 at 19:43
1
I promise that that's all the code I've used. And sorry about tidyverse, I only put it there since I know it's loaded and didn't know if it would affect the issue.
– skipndipp
Mar 25 at 19:48
1
Something is missing, of that I am confident. If you start a new R session (don't reload a cache! make sure the environment is empty withls()
), then type in your code as you present it here, you will get errors or warnings different from your question. Your assertion only makes sense (so far) if you dodf<-data.frame(...)
and thendf<-xtabs(...,data=df)
. Further, if it is the table and not the frame,df["Republican",,drop=F]
gets you the republicans, not the males (or females), so your summary statistic is mis-informing.
– r2evans
Mar 25 at 19:53
df["Republican",]
only works if there are row names, your sample data does not. Do you mean df[ df$female == "Republican",,drop=FALSE]
? (Does that code work on the console, regardless of rmarkdown? I suggest when making your rmarkdown documents, you try things on the console before trying to automate and render your report.)– r2evans
Mar 25 at 19:38
df["Republican",]
only works if there are row names, your sample data does not. Do you mean df[ df$female == "Republican",,drop=FALSE]
? (Does that code work on the console, regardless of rmarkdown? I suggest when making your rmarkdown documents, you try things on the console before trying to automate and render your report.)– r2evans
Mar 25 at 19:38
The code I provided works well for me on the console :/ It automatically generated row names for me. Your solution gives me this error: "Error in df$female : $ operator is invalid for atomic vectors"
– skipndipp
Mar 25 at 19:41
The code I provided works well for me on the console :/ It automatically generated row names for me. Your solution gives me this error: "Error in df$female : $ operator is invalid for atomic vectors"
– skipndipp
Mar 25 at 19:41
There must be code that you are not sharing, since that error occurs when
df
is not a frame. Are you by chance overwriting with df <- xtabs(...)
? That's rather important. More the point, data.frame
does not auto-generate row names like that, and that's the only place you are assigning to df
.)– r2evans
Mar 25 at 19:43
There must be code that you are not sharing, since that error occurs when
df
is not a frame. Are you by chance overwriting with df <- xtabs(...)
? That's rather important. More the point, data.frame
does not auto-generate row names like that, and that's the only place you are assigning to df
.)– r2evans
Mar 25 at 19:43
1
1
I promise that that's all the code I've used. And sorry about tidyverse, I only put it there since I know it's loaded and didn't know if it would affect the issue.
– skipndipp
Mar 25 at 19:48
I promise that that's all the code I've used. And sorry about tidyverse, I only put it there since I know it's loaded and didn't know if it would affect the issue.
– skipndipp
Mar 25 at 19:48
1
1
Something is missing, of that I am confident. If you start a new R session (don't reload a cache! make sure the environment is empty with
ls()
), then type in your code as you present it here, you will get errors or warnings different from your question. Your assertion only makes sense (so far) if you do df<-data.frame(...)
and then df<-xtabs(...,data=df)
. Further, if it is the table and not the frame, df["Republican",,drop=F]
gets you the republicans, not the males (or females), so your summary statistic is mis-informing.– r2evans
Mar 25 at 19:53
Something is missing, of that I am confident. If you start a new R session (don't reload a cache! make sure the environment is empty with
ls()
), then type in your code as you present it here, you will get errors or warnings different from your question. Your assertion only makes sense (so far) if you do df<-data.frame(...)
and then df<-xtabs(...,data=df)
. Further, if it is the table and not the frame, df["Republican",,drop=F]
gets you the republicans, not the males (or females), so your summary statistic is mis-informing.– r2evans
Mar 25 at 19:53
|
show 2 more comments
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%2f55345161%2fsubscript-out-of-bounds-error-when-trying-to-knit-to-md-file-how-to-fix%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
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
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%2f55345161%2fsubscript-out-of-bounds-error-when-trying-to-knit-to-md-file-how-to-fix%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
df["Republican",]
only works if there are row names, your sample data does not. Do you meandf[ df$female == "Republican",,drop=FALSE]
? (Does that code work on the console, regardless of rmarkdown? I suggest when making your rmarkdown documents, you try things on the console before trying to automate and render your report.)– r2evans
Mar 25 at 19:38
The code I provided works well for me on the console :/ It automatically generated row names for me. Your solution gives me this error: "Error in df$female : $ operator is invalid for atomic vectors"
– skipndipp
Mar 25 at 19:41
There must be code that you are not sharing, since that error occurs when
df
is not a frame. Are you by chance overwriting withdf <- xtabs(...)
? That's rather important. More the point,data.frame
does not auto-generate row names like that, and that's the only place you are assigning todf
.)– r2evans
Mar 25 at 19:43
1
I promise that that's all the code I've used. And sorry about tidyverse, I only put it there since I know it's loaded and didn't know if it would affect the issue.
– skipndipp
Mar 25 at 19:48
1
Something is missing, of that I am confident. If you start a new R session (don't reload a cache! make sure the environment is empty with
ls()
), then type in your code as you present it here, you will get errors or warnings different from your question. Your assertion only makes sense (so far) if you dodf<-data.frame(...)
and thendf<-xtabs(...,data=df)
. Further, if it is the table and not the frame,df["Republican",,drop=F]
gets you the republicans, not the males (or females), so your summary statistic is mis-informing.– r2evans
Mar 25 at 19:53