小编ada*_*ith的帖子

将空白插入载体中,例如R中的次要刻度标签

这个问题一般涉及先前关于在轴上创建次刻度标记的SO问题ggplot2,特别是对该问题的答案中的注释,表明将空白插入序列的功能可能证明是有用的.

由于我经常在类似的情节中添加小刻度标记,因此我尝试创建这样的函数(请参阅下面的答案).

plot r vector sequence axis-labels

11
推荐指数
1
解决办法
1924
查看次数

在data.table :: fread中绕过"ghost"换行符或文件结尾(EOF)

我正在加载从(可访问我)数据库导出的几个大的制表符分隔文本文件到R使用data.table::fread. fread处理大多数文件非常容易和速度,但其中一个文件正在生成一个定期报告的fread错误:

Error in fread(read_problem, encoding = "UTF-8", na.strings = "", header = TRUE,  : 
                   Expected sep ('  ') but new line or EOF ends field ...
Run Code Online (Sandbox Code Playgroud)

此处提供了包含违规行的较小(2000行)版本的文件(RDS文件).

以下是我尝试诊断问题的方法:

library(data.table) # I'm using 1.9.7 development (same error with 1.9.6)
read_problem <- readRDS("read_problem.rds")
error <- fread(read_problem, encoding = "UTF-8", na.strings = "",
               header = TRUE, sep = "\t", 
               colClasses = rep("character", 44),  # For simplicity
               verbose = TRUE)
Run Code Online (Sandbox Code Playgroud)

如果我删除违规行,问题就会消失:

cat(read_problem, file = "temp")   
string_vec …
Run Code Online (Sandbox Code Playgroud)

error-handling r dataframe data.table

7
推荐指数
1
解决办法
3624
查看次数

mgcv:为“全局”分组平滑指定因子平滑交互

我找到了Pedersen 等人的Hierarchical GAM 预印本(和GH repo )。对于功能反应的组间变异建模非常有帮助,但我遇到了绊脚石。

我有一些时间序列数据(随着时间的推移计数),具有以下基本期望:

  1. 计数和时间之间存在感兴趣的全局功能响应;
  2. 全局函数在几个“固定”组 (2-5) 之间变化,可能具有类似的平滑参数;
  3. 每年与该全局函数的“随机”偏差似乎存在系统趋势(即,功能响应的形状朝一致的方向漂移);但
  4. 年度偏差趋势对"固定"组的应用有所不同。

在这里,我尝试用一​​个人为的例子来说明,因为我担心我的描述不够充分。我为两个“固定”组/治疗模拟 20 个重复(年度)时间序列。在对照(trt == 0)中,年度变化很小。在治疗中(trt == 1),响应的形状存在系统趋势。

library(reshape)
library(dplyr)
library(ggplot2)
set.seed(2020)
n_yr <- 20
n_trt <- 2
n_x <- 10
dat <- tibble(yr = rep(seq(0, n_yr - 1), n_trt)) %>%
  expand.grid.df(tibble(x = rep(seq(0, 1, length.out = 10), n_trt),
                        tweak = c(rep(0, 15), rep(0.02, 5)),
                        trt = rep(0:1, each = n_x)), .) %>%
  mutate(e = rnorm(n_x * n_yr * n_trt, 0, 0.3),
         y = …
Run Code Online (Sandbox Code Playgroud)

r time-series smoothing gam mgcv

5
推荐指数
0
解决办法
771
查看次数

基于字符向量进行子集设置时knit_expand失败

我本质上是试图修改此答案,以编程方式为每个变量级别的图生成块。

但是,在我的特定情况下,我传递了一个用于后续子集的字符向量,这似乎是代码失败的根源。

# My report (test.Rmd)

```{r}
library(ggplot2)
library(knitr)
data(diamonds)
diamonds$cut <- factor(gsub(" ", "_", diamonds$cut)) # Get rid of spaces
cut.levels <- levels(diamonds$cut)
```

## Generate report for each level of diamond cut
```{r, include=FALSE}
src <- lapply(cut.levels, function(cut) knit_expand(file = "template.Rmd"))
```

`r knit(text = unlist(src))`
Run Code Online (Sandbox Code Playgroud)

和模板(template.Rmd):

```{r, results='asis', echo = FALSE}
cat("### {{cut}} cut")
```

```{r {{cut}}-cut, eval = FALSE}
with(subset(diamonds, cut == "{{cut}}"), 
     plot(carat, price, main = paste("{{cut}}", "cut"))
)

```
Run Code Online (Sandbox Code Playgroud)

将其与template.Rmd中的第二个块一起运行以eval=FALSE产生预期的输出-每个块的一系列标头以及所回显的代码。但是,调用中 …

r subset knitr

3
推荐指数
1
解决办法
152
查看次数