Reeza
Reeza

Reputation: 53

Creating a table in RStudio using vectors

I'd like to create the following table in R Studio:

  Students           Percentage Financial Assistance
1 with_Pell_Grant    0.4059046  True                
2 without_Pell_Grant 0.5018954  True                
3 with_loan          0.4053371  False               
4 without_loan       0.2290538  False      

Here it is my code:

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

setNames(Students1, Percentage1, financial_assistance1)
as.table(setNames(Students1, Percentage1, financial_assistance1))

But I am getting the following error:

Error in setNames(Students1, Percentage1, financial_assistance1) : 
  unused argument (financial_assistance1) 

My goal is to have a functional table, so I can create useful visualizations with ggplot2.

ggplot(data = comparison_table) + 
  geom_bar(mapping = aes(x = Students, fill = financial_assistance1))

But it didn't work when I generated the table this way:

comparison_table <- matrix(c("with_Pell_Grant","without_Pell_Grant","with_loan", "without_loan", 0.4059046, 0.5018954, 0.4053371, 0.2290538, 'True', 'True', 'False', 'False'),ncol=3,byrow=FALSE)
 colnames(comparison_table) <- c("Students", "Percentage", "Financial Assistance")
 rownames(comparison_table) <- c('1','2','3', '4')
 comparison_table <- as.table(comparison_table)
 comparison_table

What would be the correct way of doing this?

Upvotes: 0

Views: 2117

Answers (1)

niko
niko

Reputation: 5281

What about

dat <- data.frame(Students1, Percentage1, financial_assistance1, stringsAsFactors = F)
> dat
           Students1 Percentage1 financial_assistance1
1    with_Pell_Grant   0.4059046                  True
2 without_Pell_Grant   0.5018954                  True
3          with_loan   0.4053371                 False
4       without_loan   0.2290538                 False

Or

> cbind.data.frame(Students1, Percentage1, financial_assistance1)
           Students1 Percentage1 financial_assistance1
1    with_Pell_Grant   0.4059046                  True
2 without_Pell_Grant   0.5018954                  True
3          with_loan   0.4053371                 False
4       without_loan   0.2290538                 False

Upvotes: 1

Related Questions