This question already has answers here:
Adjusting width of tables made with kable() in RMarkdown documents
(5 answers)
Closed 5 years ago.
When I output my R Markdown (knitr / RStudio) to html the following table stretches the full width of the browser you view it in. It's only two columns and looks rather odd stretched out on a widescreen display.
Col1 | Col2
--- | ---
1 | 1
1349 | 143910
This same table shown below, same syntax, correctly limits the width of Column 1 to the width of its own contents. The only difference is the cell contents of position [2, 2] are extremely long.
Col1 | Col2
--- | ---
1 | 1
1349 | 143910143910143910143910143910143910143910143910143910
How do I force knitr or pandoc or R or whatever to limit column width to only slightly larger than the columns contents. Why did the extreme number of characters in cell [2, 2] force my output to behave as I wish? I didn't involve any CSS in the second table and prefer not to mess around with CSS.
I suggest you use kable from the knitr package and kable_styling from the kableExtra package.
Supposing your dataframe is df
kable(df, "html") %>%
kable_styling(full_width = F)
You can find more info here.
https://www.rdocumentation.org/packages/kableExtra/versions/0.6.1/topics/kable_styling
EDIT: eipi10´S clarifications on packages. (Thanks!)
Related
I want to concatenate my two tables to see them in well aligned.
Here's an exemple
[table 1]
ID Nb Name
1 2500 Alex
2 3001 Sam
[Table 2]
ID Nb2 Name
1 1201445 Lea
2 14120 Remy
When I try to knit my table I have this:
ID Nb2 Name
1 2500 Alex
2 3001 Sam
ID Nb Name
1 1201445 Lea
2 14120 Remy
But I want to have this:
ID Nb Name
1 2500 Alex
2 3001 Sam
ID Nb2 Name
1 1201445 Lea
2 14120 Remy
Analyzing the code you provided (I have added packages that are needed):
require(dplyr)
require(flextable)
regulartable(mydataframe) %>% theme_zebra() %>% autofit()
I can see that you are using autofit() function, which sets the height and width of the columns automatically to fit best to data in the dataframe, and as I understand - you do it separately for each table. To solve this formatting issue, you don't need to concatenate those dataframes, but rather you could set the same width of columns for each table, which would look like this:
regulartable(mydataframe) %>% theme_zebra() %>% width(width = 1)
Depending on what kind of report you are creating, you should consider what type of data will be displayed in your tables and adjust the width parameter in the width function. It is worth mentioning that the width parameter takes a number representing the number of inches that your column should be.
I strongly recommend you reading documentation of flextable package to get further information on formatting those tables of yours.
This question already has answers here:
Drop unused factor levels in a subsetted data frame
(16 answers)
Closed 5 years ago.
Hello I am using R studio to filter out varieties of wine that appear less that 5000 times in a dataset.
I have run the below function -
#create new data frame with varities greater than 5000
wineVar <- setDT(wineNew)[, if(.N > 5000) .SD, by = variety]
#list the unique varieties to show theres only 5
unique(wineVar$variety)
However when I try to see how many levels there are I still get the other 632 values.
[1] Cabernet Sauvignon Pinot Noir Chardonnay
[4] Bordeaux-style Red Blend Red Blend
632 Levels: Žilavka Agiorgitiko Aglianico Aidani Airen Albana AlbarÃn ... Zweigelt
Is there a way to completely remove these as it is causing issues with my training set - ie the training set still sees the values but with no data for dropped varieties.
I think what you are looking for is this. You almost there.
wineVar <- setDT(wineNew)
wineVar <- wineVar[, .SD[.N > 5000], by = variety]
wineVar[, Variety:=as.factor(as.character(Variety))]
Let's assume I want to generate a template using R markdown. Let's also assume I have some R objects I want to paste in the Rmarkdown document.
For example, the R objects I have are:
dose = 10
units = "mg"
If I want to write a sentence like:
The dose administered was 10 mg every 3 days.
I can use:
```{r}
paste("The dose administered was",dose,units,"every 3 days.")
```
However, the output will be:
## [1] The dose administered was 10 mg every 3 days.
I know I can remove the "##" using comment=NA.
Is there any way to remove also "[1]"?
Is there any other and more efficient way to insert R objects with text using R markdown?
Thanks in advance,
Don't use an R chunk, use inline code
The dose administered was `r paste(dose, units)` every 3 days.
The function cat() makes the job :)
I have a big dataset something like this below:
Image | Length | Angel
--------------------------------
DSC_001 | 233.22 |2.00
--------------------------------
DSC_001 | 24.897 |1.2
--------------------------------
DSC_001 | 28.55 |2.87
--------------------------------
DSC_002 | 23.76 |3.71
--------------------------------
DSC_002 | 34.21 |3.21
---------------------------------
I want to do average of Length and Angles for each set (DSC_001 is one set, DSC_002 is another and so on).
I can do it manually in Excel but taking huge time when it around 4000 data point.
I like to know how I do it in R or in Excel in much smarter way?
In R, we can use dplyr
library(dplyr)
df1 %>%
group_by(image) %>%
summarise_each(funs(mean))
Or with data.table
library(data.table)
setDT(df1)[, lapply(.SD, mean) , by = image]
Or using aggregate from base R
aggregate(.~image, df1, FUN = mean)
In Excel:
Make a new list with the unique values in the Image column as decribed here.
Add the column names above your new list (not mandatory but important for clear presentation of the data).
Use AVERAGEIF() to compute a conditioned average with the formula: =AVERAGEIF(A2:A10,E3,B2:B10) assuming A2:A10 is the column Image, B2:B10 is The column of the values to calculate their mean, and E3 is the cell where the Image to calculate its' mean is stored.
Here is a screenshot to clarify this:
Hope it helps ;)
Have a question on using xtable with Sweave when there are multiple columns. A table I am working on has about 25 columns and 5 rows. The exact number of columns is not known as that is dynamic.
When I run say,
table1 <- table (df$someField)
I get a table that essentially exceeds the page length.
ColA ColB ColC
---------------------------
RowA 1 2 3 ......
RowB 3 4 6 ......
If a do a xtable on this, and run it through Sweave,
xtable(table1, caption="some table")
it overflows.
What I am looking for is something like,
ColA ColB ColC
---------------------------
RowA 1 2 3
RowB 3 4 6
ColD ColE ColF
---------------------------
RowA 11 9 34
RowB 36 8 65
with the \hline etc markups. Basically, split the xtable into parts by say 5 columns per "sub-table".
I am also running this in a batch job, so I won't be able to make changes to individual files, whatever the solution it has to be able to be generated by running Sweave on the Rnw file.
Thanks in advance,
Regards,
Raj.
Here's an example of this from ?latex.table.by in the taRifx package. You can brew something similar using longtable in LaTeX and use the latex.table.by code as a prototype.
my.test.df <- data.frame(grp=rep(c("A","B"),10),data=runif(20))
library(xtable)
latex.table.by(my.test.df)
# print(latex.table.by(test.df), include.rownames = FALSE, include.colnames = TRUE, sanitize.text.function = force)
# then add \usepackage{multirow} to the preamble of your LaTeX document
# for longtable support, add ,tabular.environment='longtable' to the print command (plus add in ,floating=FALSE), then \usepackage{longtable} to the LaTeX preamble
Regardless, the longtable package in LaTeX is the key.
Edit: It appears you have too many columns not too many rows. In that case, first try landscaping just that page.
In the header:
\usepackage{lscape}
Around your table:
\begin{landscape}
...
\end{landscape}
Or just use sidewaystable.
If your table is too wide to fit in one page, try the supertabular package, which from the description sounds like it might handle breaking over multiple pages based on width (but I've never used it so can't be sure).