0

I am new in coding, and I think rstudio can be difficult. Is there a simple code for me to make a table?

rng <- mean(1,2,3,4,5)

| "Apple" | "Pear"  | "Banana" | "Tomato" |
|---------|---------|----------|----------|
| "Orange"| 26.6%   | rng      | 138521   |

This is how I want it to look, I have tried to Google different tables but I dont understand.

camille
  • 16,432
  • 18
  • 38
  • 60
  • I don't understand. Is the data in the table supposed to be related to `rng`? Or are you looking for functions to just draw all the "|" and "-" things around values for you? Maybe this existing question can help: https://stackoverflow.com/questions/13011383/how-to-create-ascii-only-tables-as-output-in-r-similar-to-mysql-style. That's not a built-in function or anything. If we are trying to display data in a "pretty" format, it's more common to use rmarkdown or something rather than drawing ascii-text tables in raw R console output. – MrFlick Dec 08 '21 at 20:10
  • Another option here: https://stackoverflow.com/questions/15849043/how-can-i-print-a-table-in-r-with-ascii-html-or-markdown-formatting – MrFlick Dec 08 '21 at 20:11
  • Thank you! It's more of an example of how I want the table to look. I just want a code where I can write for myself what the coloum and rows are going to be. If I want to put the ```rng```in the row, then I can. If that makes sense? Sorry english is not my first language –  Dec 08 '21 at 20:17

2 Answers2

0

Try this solution:

An example:

library(kableExtra) #a table lib, for example

#our data 
df <- data.frame(col1 = c("Apple", "Orange"),
                  col2 = c("Pear","26.6%"),
                  col3 = c("Banana", "rnd"),
                  col4 = c("Tomato", "138521"))
#add mean into place
df[2,3] <- mean(1:5)

#removing colnames 
colnames(df) <- NULL

1  Apple  Pear Banana Tomato
2 Orange 26.6%      3 138521

#making a tab
kable(df, row.names = F) %>%
     column_spec (1:4, border_left = T, border_right = T) %>% 
     kable_styling()

enter image description here

manro
  • 3,529
  • 2
  • 9
  • 22
0

If what you mean is just how to create a data frame from inside R, something along these lines may work.

data.frame("Apple" = "Orange",
           "Pear" = "0.266", 
           "Banana" = mean(c(1,2,3,4,5)), 
           "Tomato" = 138521)

See also the documentation for the [tribble][1] function in the tibble package for another approach.

If it's instead about printing the data frame, the question linked in the comments should offer useful hints.

giocomai
  • 3,043
  • 21
  • 24