如何在RStudio中使用向量创建指定学生资助统计表格?
Solution to Create Your Student Financial Assistance Table
Here's how to complete your code to generate the exact table you want in RStudio:
First, let's fix the code to properly build a structured table (since setNames is for naming vectors, not creating data frames):
# Your existing vectors Students1 <- c("with_Pell_Grant","without_Pell_Grant","with_loan", "without_loan") Percentage1 <- c(0.4059046, 0.5018954, 0.4053371, 0.2290538) financial_assistance1 <- c('True', 'True', 'False', 'False') # Create a data frame with your columns student_finance <- data.frame( Students = Students1, Percentage = Percentage1, `Financial Assistance` = financial_assistance1, stringsAsFactors = FALSE # Keeps text columns as plain strings instead of factors ) # Add the "序号" (serial number) column at the beginning student_finance <- cbind(序号 = 1:nrow(student_finance), student_finance) # View the formatted table in RStudio's viewer pane View(student_finance)
Quick Explanations:
data.frame()is the standard base R function to combine vectors into a table. The backticks aroundFinancial Assistancelet us use a column name with a space.1:nrow(student_finance)automatically generates sequential numbers for the "序号" column—this works even if you add more rows later.- If you want the
Financial Assistancecolumn to be a logical type (instead of text strings), update your vector to use uppercaseTRUE/FALSEwithout quotes:financial_assistance1 <- c(TRUE, TRUE, FALSE, FALSE)
Run the full code, and you'll get a table that matches exactly what you requested. You can also print it directly to the console by typing student_finance after running the code.
内容的提问来源于stack exchange,提问作者Reeza
相关产品推荐
相关产品推荐

