我有一个数据框,可能有也可能没有某些特定的列.我想选择列,dplyr
如果它们确实存在,如果不存在,只是忽略我尝试选择它们.这是一个例子:
# Load libraries
library(dplyr)
# Create data frame
df <- data.frame(year = 2000:2010, foo = 0:10, bar = 10:20)
# Pull out some columns
df %>% select(year, contains("bar"))
# Result
# year bar
# 1 2000 10
# 2 2001 11
# 3 2002 12
# 4 2003 13
# 5 2004 14
# 6 2005 15
# 7 2006 16
# 8 2007 17
# 9 2008 18
# 10 2009 19
# 11 2010 20 …
Run Code Online (Sandbox Code Playgroud) 我习惯于将相似的任务集中到一行中。例如,如果我需要过滤a
,b
以及c
在数据表中,我把它们放在一起在一个[]
与AND运算。昨天,我注意到在我的特定情况下,它的运行速度非常慢,并且经过了测试,却没有测试链接过滤器。我在下面提供了一个示例。
首先,我为随机数生成器添加种子,加载data.table,并创建一个虚拟数据集。
# Set RNG seed
set.seed(-1)
# Load libraries
library(data.table)
# Create data table
dt <- data.table(a = sample(1:1000, 1e7, replace = TRUE),
b = sample(1:1000, 1e7, replace = TRUE),
c = sample(1:1000, 1e7, replace = TRUE),
d = runif(1e7))
Run Code Online (Sandbox Code Playgroud)
接下来,我定义我的方法。第一种方法将过滤器链接在一起。第二个将过滤器与在一起。
# Chaining method
chain_filter <- function(){
dt[a %between% c(1, 10)
][b %between% c(100, 110)
][c %between% c(750, 760)]
}
# Anding method
and_filter <- function(){
dt[a %between% c(1, …
Run Code Online (Sandbox Code Playgroud) 我不仅希望我的数字出现在我的knitr生成的报告中,而且我还希望将它们输出到单独的文件中.为此,我提供了如下代码:
```{r}
#Plot figure in report
plot(x,y)
#Plot figure in file
pdf(file="MyFig.pdf")
plot(x,y)
dev.off()
```
Run Code Online (Sandbox Code Playgroud)
这很好用,但我希望有一个更优雅的解决方案,已经内置到knitr.是否有一个块选项或类似的东西可以实现相同的结果?
我对R Markdown有些新意,所以如果这很愚蠢,请道歉.我正在准备一个使用R Markdown/Knitr的pdf报告,并遇到一个问题,如果我尝试使用kable包含一个样式表,它会将表移动到页面底部.
以下是代码示例:
---
title: "Testing"
output:
pdf_document:
fig_caption: yes
tables: true
---
```{r setup, include=FALSE}
library(knitr)
library(kableExtra)
```
Section 1: In Table \ref{fig:table1} below...
```{r table1, echo=FALSE}
kable(head(mtcars[,1:4],4), format = "latex", align = "c", caption ="\\label{fig:table1}Table Caption") %>%
column_spec(1, bold = T, width = "6em") %>%
kable_styling(position = "center")
```
Section 2: Lorem ipsum dolor sit amet
Run Code Online (Sandbox Code Playgroud)
这个输出的PDF有"第1部分"和"第2部分"相互跟随,表格位于页面的最底部.
我已经尝试删除标题,并在块的开头使用fig.cap.虽然我的表保持在应有的位置,但它根本不会生成标题,并且对该图的引用变为?? .
什么有效:摆脱kableExtra,将格式从latex更改为pandoc.该表保留在应有的位置,并且我得到了标题,但是表格丢失了我真正想要的报告格式.
我究竟做错了什么?谢谢你的帮助!
编辑:对不起!我希望桌子显示我放在哪里(在"第1节"之后,在"第2节"之前).如下所示,仅带有标题/图形标签.(链接的图像是我得到的,如果我摆脱上面的代码块中的标题arg)
我有以下数据框:
# Dummy data frame
df <- expand.grid(x = 1:3, y = 1:3)
Run Code Online (Sandbox Code Playgroud)
我想把它描绘成一个像这样的geom_tile
使用ggplot2
:
# Tile plot
ggplot(df) +
geom_tile(aes(x = x, y = y),
fill = NA, colour = "red", size = 3, width = 0.7, height = 0.7)
Run Code Online (Sandbox Code Playgroud)
这使,
但请注意,在每个瓷砖的左上角,缺少一个缺口,其中边框不能正确地吻合.如果我使用,我会得到相同的结果geom_rect
.有没有解决方法来避免这种情况?
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other …
Run Code Online (Sandbox Code Playgroud) 假设我有以下数据框:
# Set seed for RNG
set.seed(33550336)
# Create toy data frame
loc_x <- c(a = 1, b = 2, c = 3)
loc_y <- c(a = 3, b = 2, c = 1)
scaling <- c(temp = 100, sal = 10, chl = 1)
df <- expand.grid(loc_name = letters[1:3],
variables = c("temp", "sal", "chl"),
season = c("spring", "autumn")) %>%
mutate(loc_x = loc_x[loc_name],
loc_y = loc_y[loc_name],
value = runif(nrow(.)),
value = value * scaling[variables])
Run Code Online (Sandbox Code Playgroud)
看起来像,
# > head(df)
# loc_name …
Run Code Online (Sandbox Code Playgroud) 我试图通过使用apply
将用户定义的函数应用于矩阵来避免使用循环.我遇到的问题是我的函数使用了其他参数,它们对于矩阵的每一列都不同.以下是一个玩具示例.
说我有以下功能:
foo <- function(x, a, b, c) return( (a*x + b)^c )
Run Code Online (Sandbox Code Playgroud)
我想将它应用到一个矩阵bar
使用的不同的价值观a
,b
以及c
对每一列.
bar <- matrix(1:15, ncol = 3)
a <- 4:6
b <- 3:1
c <- 1:3
Run Code Online (Sandbox Code Playgroud)
在这种情况下,对于第一列bar
,然后a=4
,b=3
和c=1
.我试过这个,
apply(bar, 2, foo, a=a, b=b, c=c)
Run Code Online (Sandbox Code Playgroud)
但这显然是不正确的,因为每个列在重新包装回第一个参数之前依次使用所有参数.有什么建议?
首先,为这个例子道歉,但我找不到更好的数据集来证明这个问题。希望,这就足够了。假设我正在尝试从mtcars
绘制 mpg 与位移的数据集中制作一个传动方面的网格(自动与手动)和齿轮数,如下所示:
# Load library
library(ggplot2)
# Load data
data(mtcars)
# Plot data
p <- ggplot(mtcars,aes(x = disp, y = mpg)) + geom_point() + facet_grid(gear ~ am)
p <- p + geom_smooth()
print(p)
Run Code Online (Sandbox Code Playgroud)
这使,
注意,我添加了一条趋势线geom_smooth
,它默认使用黄土曲线。我可以拟合用户定义的函数而不是nls
用于该方法的黄土曲线 ,然后陈述一个公式,这很棒。但是是否可以为每个方面拟合不同的用户指定曲线?例如,左上角的线性回归和右下角的衰减指数。这可能吗?还是我用锤子打螺丝?
编辑:此处给出了自定义(即用户定义)拟合函数的解决方案。
我想使用ggplot2
's创建一个图形facet_grid
,如下所示:
# Load ggplot2 library for plotting
library(ggplot2)
# Plot dummy data
p <- ggplot(mtcars, aes(mpg, wt))
p <- p + geom_point()
p <- p + facet_grid(gear ~ cyl)
print(p)
Run Code Online (Sandbox Code Playgroud)
这很好,但由于它出现在期刊文章中,因此每个面板也需要标有 a、b、c 等。该包egg
有一个很好的函数调用tag_facet
,其用法如下:
# Load egg library for tagging
library(egg)
#> Warning: package 'egg' was built under R version 3.5.3
#> Loading required package: gridExtra
# Same plot but with tags for each facet
p <- ggplot(mtcars, aes(mpg, wt))
p <- p …
Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的 data.table:
# Load packages
library(data.table)
# Set RNG seed
set.seed(-1)
# Create dummy data
dt <- data.table(foo = sample(letters[1:10], 6),
bar = sample(letters[1:10], 6))
dt
#> foo bar
#> 1: g a
#> 2: h j
#> 3: j e
#> 4: a i
#> 5: d g
#> 6: i c
Run Code Online (Sandbox Code Playgroud)
我想将所有相关元素组合在一起。我的意思是,例如,a
和g
一起在第一行,所以它们属于一个组 ( a
, g
)。但是a
和i
一起在第 4 行,所以i
也属于这个组 ( a
, g
, i
)。此外, …
r ×9
ggplot2 ×4
facet-grid ×3
knitr ×2
r-markdown ×2
apply ×1
data.table ×1
dplyr ×1
facet ×1
figures ×1
kable ×1
select ×1