Error in data.frame — replacement has [x] rows, data has [y] only when knitting to HTML

22 Views Asked by At

I have an R Markdown file that I am trying to knit to HTML with knitr. In processing the file, it keeps getting hung up on a particular block of code. What I am trying to do is to filter the master data file, REDCapTab, using dplyr. I then make a new data frame (called gestage here) with one column and 10768 rows. I populate this column with the values of 16 columns from the master data file, essentially stacking these 16 columns into one. Each of the 16 columns has 673 rows and this matches the 10768 rows in the new data frame I made. I then convert this column in gestage to a factor with three levels and generate a bar plot. When I run all this code on the console, I don't get any error and I'm able to generate the bar plot without a problem. However, the problem comes when I try to knit this file to HTML. Below is my code from the R Markdown file where the error is occurring.

x <- REDCapTab %>% 
  filter(PTDbloods != 0 & (AllPTDhx == 1 | AllPTDhx == 4))
gestage <- data.frame(new_column = rep(NA, 10768))
gestage$new_column <- c(x$p1wk09, x$p2wk09, x$p3wk09, x$p4wk09, x$p5wk09, x$p6wk09, x$p7wk09, x$p8wk09, x$p9wk09, x$p10wk09, x$p11wk09, x$p12wk09, x$p13wk09, x$p14wk09, x$p15wk09, x$p16wk09)
gestage$new_column <- factor(gestage$new_column, levels = c("20-27 weeks", "28-31 weeks", "32-36 weeks"))

gest_age_sPTL <- barplot(table(gestage$new_column),
                    xlab = "Gestational Age",
                    ylab = "Frequency (#)",
                    ylim = c(0, 600),
                    col = "darkslategray2",
                    main = md("Gestational Age Distribution for Pure sPTL Cases (n = 673)"))
rm(list = c("gestage", "gest_age_sPTL", "x"))

Below is the error that I get:

processing file: NHS2CaseSelection.Rmd
                                                                                                                                       

Quitting from lines 584-597 [gest_age_pPROM] (NHS2CaseSelection.Rmd)
Error in `$<-.data.frame`:
! replacement has 0 rows, data has 5712
Backtrace:
 1. base::`$<-`(`*tmp*`, new_column, value = `<chr>`)
 2. base::`$<-.data.frame`(`*tmp*`, new_column, value = `<chr>`)
Execution halted

What I find strange is that no problem comes up in the console, just during the knitting process. I also have a very similar chunk of code immediately before this doing a similar process and no error comes up in knitting to HTML. I've attached that bit of code below in case it could help.

x <- filter(REDCapTab, PTDbloods != 0)
gestage <- data.frame(new_column = rep(NA, 40688))
gestage$new_column <- c(x$p1wk09, x$p2wk09, x$p3wk09, x$p4wk09, x$p5wk09, x$p6wk09, x$p7wk09, x$p8wk09, x$p9wk09, x$p10wk09, x$p11wk09, x$p12wk09, x$p13wk09, x$p14wk09, x$p15wk09, x$p16wk09)
gestage$new_column <- factor(gestage$new_column, levels = c("20-27 weeks", "28-31 weeks", "32-36 weeks"))

gest_age <- barplot(table(gestage$new_column),
        xlab = "Gestational Age",
        ylab = "Frequency (#)",
        ylim = c(0, 3000),
        col = "darkslategray2",
        main = "Gestational Age Distribution of Preterm Delivery Cases")
rm(list = c("gestage", "gest_age", "x"))

So far, I've tried creating my new data frame, gestage, in a variety of ways. I've tried making a vector with the column values using the rep function and then making the data frame with one column using the vector. I also tried applying the as.data.frame function on this vector. I also attempted to make the data frame using a sequence. No technique has worked and all generate the same error as above. It's worth mentioning that all these alternative techniques that I've tried have no problem running on the console, just when trying to knit to HTML.

Thank you for the help!

0

There are 0 best solutions below