“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;








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!










share|improve this question
























  • 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












  • 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





    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 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


















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!










share|improve this question
























  • 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












  • 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





    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 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














0












0








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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












  • 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





    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 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


















  • 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












  • 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





    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 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

















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













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
);



);













draft saved

draft discarded


















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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현