Home > Blockchain >  R make a simple table
R make a simple table

Time:12-09

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.

CodePudding user response:

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

CodePudding user response:

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.

  •  Tags:  
  • r
  • Related