Column name formatting in KableExtra in R - r

Latex command are not formatting the column names as I intended.
library(kableExtra)
kable(test,"latex", col.names = c('Mean','\\textit{N}' ,'Strongly\nDisagree','Disagree','Neither Agree\norDisagree','Agree','Strongly\nAgree'))
The output I am getting is:
https://www.dropbox.com/s/xvl7lfh94bl2274/Kable%20Table.PNG?dl=0
I have tried both latex commands and R-markdown commands.
The N should be italicized and Strong disagree, Neither Agree or Disagree, and Strongly Agree should be broken up on two lines.

test_data <- data.frame(Mean = runif(5, 3.71, 7.72),
N = sample(57:59, 5, replace = TRUE),
sd = c(1, rep(0, 4)),
d = rep(1, 5),
naod = sample(1:4, 5, replace = TRUE),
a = sample(5:12, 5, replace = TRUE),
sa = sample(37:44, 5, replace = TRUE))
kable(test_data,"latex" ,booktabs=T,
align="c",
col.names=linebreak(c('Mean','\\textit{N}' ,'Strongly\n Disagree','Disagree','Neither Agree\n or Disagree','Agree','Strongly\n Agree')),
row.names = T,escape=F) %>%
row_spec(0,align = "c")

Related

unused arguments when trying to create a matrix in R

I want create such matrix
dat <- matrix(
"an_no" = c(14, 17),
"an_yes" = c(3, 1),
row.names = c("TL-MCT-t", "ops"),
stringsAsFactors = FALSE
)
but i get error unused arguments.
What i did wrong and how perform correct matrix with such arguments?
as.matrix didn't help.
Thanks for your help.
You are using the arguments that you would use to build a data frame. If you want a matrix using this syntax you can do:
dat <- as.matrix(data.frame(
an_no = c(14, 17),
an_yes = c(3, 1),
row.names = c("TL-MCT-t", "ops")))
dat
#> an_no an_yes
#> TL-MCT-t 14 3
#> ops 17 1
You don't need the stringsAsFactors = FALSE because none of your data elements are strings, and in any case, stringsAsFactors is FALSE by default unless you are using an old version of R. You also don't need quotation marks around an_no and an_yes because these are both legal variable names in R.
The matrix function estructure is this:
matrix(data = NA,
nrow = 1,
ncol = 1,
byrow = FALSE,
dimnames = NULL)
Appears you're trying to create a data.frame
data.frame(row_names = c("TL-MCT-t", "ops"),
an_no = c(14,17),
an_yes = c(3,1)
)

R noob: running a simple 7-variable CSV through a bvarsv model

My simple code is yielding: Error in dimnames<-.data.frame(*tmp*, value = list(n)) :
invalid 'dimnames' given for data frame
Any help appreciated
library(bvarsv)
library(tidyverse)
library(janitor)
library(readxl)
set.seed(1)
test = read_excel("Desktop/test.csv")
bvar.sv.tvp(test, p = 2, tau = 40, nf = 10, pdrift = TRUE, nrep = 50000,
nburn = 5000, thinfac = 10, itprint = 10000, save.parameters = TRUE,
k_B = 4, k_A = 4, k_sig = 1, k_Q = 0.01, k_S = 0.1, k_W = 0.01,
pQ = NULL, pW = NULL, pS = NULL)
Edit:
The documentation specifies:
Y - Matrix of data, where rows represent time and columns are
different variables.Y must have at least two columns.
So when you read in your dataset, time will be a column at first, meaning you have to transform the dataframe that the time column will be your rownames. (Maybe you also want to use the lubridate package to parse your time column first).
tst <- read.csv("Desktop/tst.csv", TRUE, ",")
# tst_df <- data.frame(tst) # Should not be necassary
rownames(tst_df) <- tst_df[,1]
tst_df[,1] <- NULL
bvar.sv.tvp(tst_df, ...)
You can also the usmacro dataset as an example to see how the input data of bvar.sv.tvp() should look like.
data(usmacro)
print(usmacro)
Original Post:
I don't know how your csv looks like. So it is hard to tell what the actual issue is.
But you can try wrapping your data in "as.data.frame(test)" like this:
bvar.sv.tvp(as.data.frame(test), p = 2, tau = 40, nf = 10, pdrift = TRUE, nrep = 50000,
nburn = 5000, thinfac = 10, itprint = 10000, save.parameters = TRUE,
k_B = 4, k_A = 4, k_sig = 1, k_Q = 0.01, k_S = 0.1, k_W = 0.01,
pQ = NULL, pW = NULL, pS = NULL)

For loop list of tibbles R

I want to create a list of random tibbles using a for loop. I have a large data set where I will need to apply functions to lists of tibbles and create lists of tibbles as the outputs. I understand there might be better ways to do this and would also appreciate hearing those but am trying to wrap my head around how for loops work.
I can create a list of random tibbles with each tibble in the list named:
tibble_random1 <- tibble(Number = sample((1:100), 10, replace = TRUE),
Letter = sample((LETTERS), 10, replace = TRUE),
Logical = sample(c("True", "False"), 10, replace = TRUE))
tibble_random2 <- tibble(Number = sample((1:100), 10, replace = TRUE),
Letter = sample((LETTERS), 10, replace = TRUE),
Logical = sample(c("True", "False"), 10, replace = TRUE))
tibble_random3 <- tibble(Number = sample((1:100), 10, replace = TRUE),
Letter = sample((LETTERS), 10, replace = TRUE),
Logical = sample(c("True", "False"), 10, replace = TRUE))
tibble_random <- list(tibble1 = tibble_random1,
tibble2 = tibble_random2,
tibble3 = tibble_random3)
I cannot figure out how to do this with a for loop or if a for loop is completely inappropriate for this.
Thanks.
Initialise a list and fill 1 tibble in every iteration using for loop.
tibble_random <- vector('list', 3)
for(i in seq_along(tibble_random)) {
tibble_random[[i]] <- tibble(Number = sample((1:100), 10, replace = TRUE),
Letter = sample((LETTERS), 10, replace = TRUE),
Logical = sample(c("True", "False"), 10, replace = TRUE))
}
You can also use replicate or lapply to do this without for loop.
tibble_random <- replicate(3, tibble(Number = sample((1:100), 10, replace = TRUE),
Letter = sample((LETTERS), 10, replace = TRUE),
Logical = sample(c("True", "False"), 10, replace = TRUE)), simplify = FALSE)
To assign the names of the list you can use :
names(tibble_random) <- paste0('tibble', seq_along(tibble_random))

data manipulation - R

I am struggling with data manipulation in R. My dataset consists of variables type(5 factors), intensity(3 factors), damage(continous). I want to calculate mean damage(demage1, demage2 and damage3 separately) with respect to intensity and type. In onther words I want to summarize the average damage by type and intensity. I have created this small reproducible example of my data:
type <- sample(seq(from = 1, to = 5, by = 1), size = 50, replace = TRUE)
intensity <- sample(seq(from = 1, to = 3, by = 1), size = 50, replace = TRUE)
damage1 <- sample(seq(from = 1, to = 50, by = 1), size = 50, replace = TRUE)
damage2 <- sample(seq(from = 1, to = 200, by = 1), size = 50, replace = TRUE)
damage3 <- sample(seq(from = 1, to = 500, by = 1), size = 50, replace = TRUE)
dat <- cbind(type, intensity, damage1, damage2, damage3)
then to manipulate the data I have used the pipe operator %>% buy my commands seem not to work very well:
dat <- as.data.frame(dat)
dat %>%
filter(type == 1) %>%
group_by(intensity, damage) %>%
summarise(mean_damage = mean(Value))
I have read about multiple usefull functions here:
efficient reshaping using data tables
manipulating data tables
Do Faster Data Manipulation using These 7 R Packages
But I wasnt able to make any progress here. My question are:
What is wrong with my code?
Am I even going in the right direction here?
Is there some alternative how to do this?

R chunk code stay inside the Beamer frame

Here is my MWE code.
\documentclass{beamer}
\begin{document}
<<setup, include=FALSE>>=
# smaller font size for chunks
opts_chunk$set(size = 'footnotesize')
options(width=60)
#
\begin{frame}[fragile]
\frametitle{Test1}
<<boring-random>>=
y <- c(5, 7, 15, 17, 17, 19)
Trt <- gl(n = 3, k = 2, length = 3 * 2, labels = paste("Trt",
1:3, sep = ""), ordered = FALSE)
Data <- data.frame(Trt, y)
Fit1 <- aov(formula = y ~ Trt, data = Data, contrasts = list(Trt = "contr.sum"))
ANOVA1 <- anova(Fit1)
Coeffs1 <- coefficients(Fit1)
#
\end{frame}
\end{document}
I'm struggling to keep the R chunk code within the Beamer frame. I wonder what is the efficient way to manage the R chunk codes such that they stay inside the Beamer frame. Thanks
The best approach is to turn off the tidy option by tidy=FALSE, and manually break your lines.
<<boring-random, tidy=FALSE>>=
y <- c(5, 7, 15, 17, 17, 19)
Trt <- gl(n = 3, k = 2, length = 3 * 2, labels = paste("Trt",
1:3, sep = ""), ordered = FALSE)
Data <- data.frame(Trt, y)
Fit1 <- aov(formula = y ~ Trt, data = Data,
contrasts = list(Trt = "contr.sum"))
ANOVA1 <- anova(Fit1)
Coeffs1 <- coefficients(Fit1)
#
This will always work. The other way is to set smaller width in options() (knitr FAQ 8), and you probably need to try a few times for an ideal width. In your case, 60 is apparently too big.

Resources