提问人:james1233 提问时间:10/27/2023 最后编辑:james1233 更新时间:10/27/2023 访问量:32
R 代码运行,但未编织显示错误
R Code runs however it doesn't knit shows an error
问:
我的代码如下。当我去运行它时,它工作得很好。当我单击“编织”时,问题就出现了。我只是想获取我的代码的 HTML 版本。请在下面查看我的代码:
options(repos = c(CRAN = "https://cloud.r-project.org"))
library(tidyverse)
library(readxl)
breast_cancer_5_ <- read_excel("C:/Users/B/Desktop/xx/breast_cancer (5).xls")
View(breast_cancer_5_)
data <- breast_cancer_5_
library(dplyr)
colnames(data) <- c(
'Age', 'Race', 'Marital_Status', 'T_Stage', 'N_Stage',
'Stage_6th', 'Differentiate', 'Grade', 'A_Stage',
'Tumor_Size', 'Estrogen_Status', 'Progesterone_Status',
'Regional_Node_Examined', 'Regional_Node_Positive',
'Survival_Months', 'Status'
)
{r setup, echo=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(ggplot2)
# Categorize age into groups
data <- data %>%
mutate(Age_Group = cut(Age, breaks = c(30, 40, 50, 60, 70, Inf), labels = c("30-39", "40-49", "50-59", "60-69", "70+"), include.lowest = TRUE))
# Create a contingency table using table() and remove any missing values
contingency_table <- table(data$Age_Group, data$Status_Binary, useNA= "ifany")
contingency_table <- contingency_table[rowSums(contingency_table) > 0, ]
# Remove empty rows from the contingency table
contingency_table <- table(data$Age_Group, data$Status_Binary)
contingency_table <- contingency_table[rowSums(contingency_table) > 0, ]
# Print contingency table
print(contingency_table)
# bar plot
bar_plot <- data %>%
group_by(Age_Group, Status_Binary) %>%
summarize(Count = n()) %>%
ggplot(aes(x = Age_Group, y = Count, fill = Status_Binary)) +
geom_bar(stat = "identity", position = "stack") +
labs(title = "Association Between Age and Likelihood of Breast Cancer",
x = "Age Group",
y = "Count",
fill = "Likelihood of Breast Cancer") +
theme_minimal()
# Print bar plot
print(bar_plot)
我尝试了此代码,并在按下“Knit”时收到以下错误消息:
Quitting from lines 107-155 [setup] (530834021.Rmd)
Error in `table()`:
! all arguments must have the same length
Backtrace:
1. base::table(data$Age_Group, data$Status_Binary, useNA = "ifany")
Execution halted
错误消息表明,使用 table() 函数时,两个变量 Age_Group 和 Status_Binary 之间存在长度不匹配。
不确定,但要确保两个变量具有相同的观测值长度或数量。我可以使用 length() 函数检查变量的长度并进行比较。
答: 暂无答案
评论