我使用kableExtra
了longtable
下面的代码。
library(knitr)
library(kableExtra)
long_dt <- rbind(mtcars, mtcars)
kable(
long_dt,
format = "latex",
longtable = T,
booktabs = T,
caption = "Longtable"
) %>%
add_header_above(c(" ", "Group 1" = 5, "Group 2" = 6)) %>%
kable_styling(latex_options = c("repeat_header"))
Run Code Online (Sandbox Code Playgroud)
输出是
我想知道如何在表格底部添加文本(下一页继续...),如果它跨越到下一页。
我想绘制一个ggplot2
带有自定义y限制的条形图。
Type <- LETTERS[1:5]
Y <- c(99, 99.5, 99.0, 98.8, 98.5)
df <- data.frame(Type, Y)
Run Code Online (Sandbox Code Playgroud)
下面的代码适用于条形图:
library(ggplot2)
ggplot(data = df, mapping = aes(x = Type, y = Y, fill = Type)) +
geom_bar(stat = "identity") +
theme_bw()
Run Code Online (Sandbox Code Playgroud)
但是,我无法设置y限制。请参见下面的代码。
ggplot(data = df, mapping = aes(x = Type, y = Y, fill = Type)) +
geom_bar(stat = "identity") +
scale_y_continuous(limits = c(90, 100)) +
theme_bw()
ggplot(data = df, mapping = aes(x = Type, y = Y, fill = Type)) + …
Run Code Online (Sandbox Code Playgroud) 我想在字符串中的三个字符后添加空格。我使用了以下代码,效果很好。我想知道是否还有其他简单的方法来完成相同的任务
library(stringi)
Test <- "3061660217"
paste(
stri_sub(str = Test, from = 1, to = 3)
, stri_sub(str = Test, from = 4)
, sep = " "
)
[1] "306 1660217"
Run Code Online (Sandbox Code Playgroud) 我想知道如何使用以下R代码中的xtable函数获取LATEX表.
Block <- gl(8, 4)
A <- factor(c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1,0,1,0,1))
B <- factor(c(0,0,1,1,0,0,1,1,0,1,0,1,1,0,1,0,0,0,1,1,
0,0,1,1,0,0,1,1,0,0,1,1))
C <- factor(c(0,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,0,1,0,1,
1,0,1,0,0,0,1,1,1,1,0,0))
Yield <- c(101, 373, 398, 291, 312, 106, 265, 450, 106, 306, 324, 449,
272, 89, 407, 338, 87, 324, 279, 471, 323, 128, 423, 334,
131, 103, 445, 437, 324, 361, 302, 272)
aovdat <- data.frame(Block, A, B, C, Yield)
summary(aov(Yield~Block+A*B+Error(A*Block), data=aovdat))
Error: A
Df Sum Sq Mean Sq
A 1 3465.3 3465.3
Error: Block
Df Sum Sq Mean Sq …
Run Code Online (Sandbox Code Playgroud) 我用psych
包装模拟SEM(结构方程模型)的数据.我使用了使用psych包来生成和测试结构模型的第17页上给出的代码.代码是
library(psych)
set.seed(42)
fx <- matrix(c(0.9, 0.8, 0.7, rep(0, 9), 0.7, 0.6, 0.5, rep(0, 9), 0.6, 0.5, 0.4), ncol = 3)
rownames(fx) <- paste("x", 1:9, sep="")
fy <- matrix(c(0.6, 0.5, 0.4), ncol=1)
rownames(fy) <- paste("y", 1:3, sep="")
Phi <- matrix(c(1, 0.48, 0.32, 0.4, 0.48, 1, 0.32, 0.3, 0.32, 0.32, 1, 0.2, 0.4, 0.3, 0.2, 1), ncol = 4)
twelveV <- sim.structure(fx=fx, Phi=Phi, fy=fy, n=100, raw=TRUE)
round(twelveV$model, 2)
round(twelveV$model-twelveV$r, 2)
twelveV$observed
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用sem
包来分析模拟数据.代码是
sem.mod <- structure.sem(twelveV$model) …
Run Code Online (Sandbox Code Playgroud) 我用这段R
代码制作了biplot :
set.seed(12345)
require(ggplot2)
AData <- data.frame(Glabel=LETTERS[1:7], A=rnorm(7, mean = 0, sd = 1), B=rnorm(7, mean = 0, sd = 1))
TData <- data.frame(Tlabel=LETTERS[11:20], A=rnorm(10, mean = 0, sd = 1), B=rnorm(10, mean = 0, sd = 1))
i <- 2
j <- 3
p <- ggplot(data=AData, aes(AData[, i], AData[, j])) + geom_point() + theme_bw()
p <- p + geom_text(aes(data=AData, label=Glabel), size=3, vjust=1.25, colour="black")
p <- p + geom_segment(data = TData, aes(xend = TData[ ,i], yend=TData[ ,j]),
x=0, …
Run Code Online (Sandbox Code Playgroud) 我正在制作许多具有相同xlab的图表.我想我们可以在这样的主题中使用xlab:
theme_mine1 <- opts(
panel.grid.major = theme_blank()
, panel.grid.minor = theme_blank()
, panel.background = theme_blank()
, axis.line = theme_blank()
, xlab = expression(paste("My (xlab ", m^{-2}, ")"))
)
Run Code Online (Sandbox Code Playgroud)
当我使用这个主题时,它不会给出任何错误或警告,但它不会改变xlab.任何建议将受到高度赞赏.谢谢
我可以使用ginv
函数从MASS
库中得到一个矩阵的摩尔-彭罗斯Generalisied逆.
m <- matrix(1:9, 3, 3)
library(MASS)
ginv(m)
Run Code Online (Sandbox Code Playgroud)
在SAS中,我们确实有多个函数来获得矩阵的广义逆.SVD可用于找到广义逆,但这也是Moore-Penrose.我想知道在R中是否有任何函数可以得到除Moore-Penrose Generalisied Inverse之外的矩阵的广义逆(这不是唯一的).在此先感谢您的帮助和时间.
编辑
矩阵A的广义逆定义为满足等式AGA = A的任何矩阵G.
这个G不是Moore-Penrose Generalisied Inverse,所以它不是唯一的.
我正在使用knitr 0.5
我的分析,它会抛出这个警告
Warning in parse_params(params) :
(*) NOTE: I saw options " label = TrtScores-SD-Response, echo = FALSE, results = asis"
are you using the old Sweave syntax? go http://yihui.name/knitr/options
Run Code Online (Sandbox Code Playgroud)
为了这个chunck
<< label = TrtScores-SD-Response, echo = FALSE, results = asis >>=
R code
@
Run Code Online (Sandbox Code Playgroud)
并knitr 0.5
变得非常慢.有任何想法克服这个警告.谢谢
我想知道是否有任何 R 函数可以根据样本平均值、SD 和样本大小 (n) 执行 ANOVA 和 TukeyHSD。我找到了对样本统计数据执行方差分析的ind.oneway.second
函数rpsychi
,但找不到任何对样本统计数据执行 TukeyHSD 的函数。
library("rpsychi")
Mean <- c(90,85,92,100,102,106)
SD <- c(9.035613,11.479667,9.760268,7.662572,9.830258,9.111457)
SampleSize <- c(9,9,9,9,9,9)
fm1 <- ind.oneway.second(Mean, SD, SampleSize)
fm1
names(fm1)
TukeyHSD(fm1$anova.table)
Run Code Online (Sandbox Code Playgroud)