小编nac*_*dus的帖子

提取dplyr tbl列作为向量

是否有更简洁的方法将dplyr tbl的一列作为向量,从具有数据库后端的tbl(即数据帧/表不能直接是子集)?

require(dplyr)
db <- src_sqlite(tempfile(), create = TRUE)
iris2 <- copy_to(db, iris)
iris2$Species
# NULL
Run Code Online (Sandbox Code Playgroud)

这太容易了,所以

collect(select(iris2, Species))[, 1]
# [1] "setosa"     "setosa"     "setosa"     "setosa"  etc.
Run Code Online (Sandbox Code Playgroud)

但它似乎有点笨拙.

r lazy-evaluation collect dplyr

150
推荐指数
7
解决办法
6万
查看次数

使用R markdown,knitr,pandoc,beamer绘制尺寸和分辨率

默认情况下不适合幻灯片,甚至不通过任何其他方式打印.

这是.Rmd:编辑:似乎你必须在每个块中使用plot().第二个图现在打印.

# Plot should show at high resolution

```{r echo=FALSE, comment = ""}
# load some data
require(plyr)
rbi <- ddply(baseball, .(year), summarise,  
  mean_rbi = mean(rbi, na.rm = TRUE))
```

```{r}
# plot
plot(mean_rbi ~ year, type = "l", data = rbi)
```

# Second attempt
```{r, fig.width = 2, fig.height = 2}
plot(mean_rbi ~ year, type = "l", data = rbi)
```

# Third attempt
```{r, out.width = 2, out.height = 2}
plot(mean_rbi ~ year, type = …
Run Code Online (Sandbox Code Playgroud)

r beamer pandoc knitr r-markdown

58
推荐指数
2
解决办法
8万
查看次数

使用lubridate从POSIXct日期时间开始的月份的第一天

给定POSIXct日期时间,如何提取当月的第一天进行聚合?

library(lubridate)

full.date <- ymd_hms("2013-01-01 00:00:21")
Run Code Online (Sandbox Code Playgroud)

r lubridate

19
推荐指数
3
解决办法
2万
查看次数

剥去日期并保留时间

很多人都会问如何剥夺时间并保留日期,但另一方面呢?鉴于:

myDateTime <- "11/02/2014 14:22:45"
Run Code Online (Sandbox Code Playgroud)

我想看看:

myTime
[1] "14:22:45"
Run Code Online (Sandbox Code Playgroud)

时区没有必要.

我已经尝试过(从其他答案)

as.POSIXct(substr(myDateTime, 12,19),format="%H:%M:%S")
Run Code Online (Sandbox Code Playgroud)

[1]"2013-04-13 14:22:45 NZST"

目的是分析仅在一天中的几天记录的事件.

谢谢

编辑:

事实证明,没有纯粹的"时间"对象,所以每次都必须有一个日期.

最后我用了

as.POSIXct(as.numeric(as.POSIXct(myDateTime)) %% 86400, origin = "2000-01-01")
Run Code Online (Sandbox Code Playgroud)

而不是角色解决方案,因为我需要对结果进行算术运算.这个解决方案类似于我原来的解决方案,除了可以一致地控制日期 - 在这种情况下是"2000-01-01",而我的尝试只是在运行时使用了当前日期.

datetime r

15
推荐指数
2
解决办法
2万
查看次数

类似于列表列表中的expand.grid

我有三个文本文档存储为名为"dlist"的列表列表:

dlist <- structure(list(name = c("a", "b", "c"), text = list(c("the", "quick", "brown"), c("fox", "jumps", "over", "the"), c("lazy", "dog"))), .Names = c("name", "text"))
Run Code Online (Sandbox Code Playgroud)

在我的脑海中,我发现像这样的图片列表很有帮助:

   name  text
1  a     c("the", "quick", "brown")
2  b     c("fox", "jumps", "over", "the")
3  c     c("lazy", "dog")
Run Code Online (Sandbox Code Playgroud)

如何操纵如下?想法是绘制图形,因此可以为ggplot2融化的东西会很好.

  name  text
1    a   the
2    a quick
3    a brown
4    b   fox
5    b jumps
6    b  over
7    b   the
8    c  lazy
9    c   dog
Run Code Online (Sandbox Code Playgroud)

这是每个单词一行,同时给出单词及其父文档.

我试过了:

> expand.grid(dlist)
  name                  text
1    a     the, …
Run Code Online (Sandbox Code Playgroud)

r

15
推荐指数
1
解决办法
2113
查看次数

do.call("rbind",list(data,frames))还要按行原始数据帧索引每一行

df1 <- data.frame(a = 1:2, b = 3:4)
df2 <- data.frame(a = 5:6, b = 7:8)

# A common method loses the origin of each row.
do.call("rbind", list(df1, df2))
##   a b
## 1 1 3
## 2 2 4
## 3 5 7
## 4 6 8

# Whereas here, X1 records which data frame each row originated in.
library(plyr)
adply(list(df1, df2), 1)
##   X1 a b
## 1  1 1 3
## 2  1 2 4
## 3 …
Run Code Online (Sandbox Code Playgroud)

r dplyr

5
推荐指数
1
解决办法
738
查看次数

在Linux Mint上使用lua构建Vim

这就是我做的:

# Install lua
curl -R -O http://www.lua.org/ftp/lua-5.2.2.tar.gz
tar zxf lua-5.2.2.tar.gz
cd lua-5.2.2
sudo make linux install

# build vim
sudo apt-get install libncurses5-dev libgnome2-dev libgnomeui-dev \
libgtk2.0-dev libatk1.0-dev libbonoboui2-dev \
libcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev ruby-dev mercurial
sudo apt-get remove vim vim-runtime gvim
sudo apt-get remove vim-tiny vim-common vim-gui-common
cd ~
hg clone https://code.google.com/p/vim/
cd vim
./configure --with-features=huge \
            --enable-rubyinterp \
            --enable-pythoninterp \
            --with-python-config-dir=/usr/lib/python2.7-config \
            --enable-perlinterp \
            --enable-gui=gtk2 --enable-cscope --prefix=/usr \
            --enable-luainterp \
            --with-lua-prefix=/usr/local/bin/lua
make VIMRUNTIMEDIR=/usr/share/vim/vim74
sudo make install …
Run Code Online (Sandbox Code Playgroud)

vim lua build linux-mint

4
推荐指数
1
解决办法
8210
查看次数

从marrangeGrob(或rangingList)pdf中删除页码

marrangeGrobgridExtra整理grobs(通常在我的情况ggplots)的行,列和页面。它还为页面编号。

require(ggplot2)
require(plyr)
require(gridExtra)

p <- function(plotdata) {
    x <- ggplot(plotdata, aes(wt, mpg)) + 
        geom_point() + 
        ggtitle(plotdata$gear[1])
    return(x)
}

all <- dlply(mtcars, .(gear), p)

allarranged <- do.call(marrangeGrob, c(all, nrow=1, ncol=2))
ggsave("multipage.pdf", allarranged, width=12)
Run Code Online (Sandbox Code Playgroud)

那是一个愚蠢但可复制的例子。

现在检查的输出str(allarranged[[1]])以显示页码的对象。简化为必需品,它们在这里:

[[1]]
  $ children     :List of 5
  ..$GRID.frame.1925:List of 6
  .. ..$ children     :List of 5
  .. .. ..$ GRID.cellGrob.1927:List of 10
  .. .. .. ..$ children     :List of 1
  .. .. .. .. ..$ GRID.text.1845:List of 11
  .. .. .. …
Run Code Online (Sandbox Code Playgroud)

r gridextra

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

初始化一个完整的空数据框(没有行,没有列)

这是一个没有行而没有列的空数据框架:

iris[FALSE, FALSE]
#> data frame with 0 columns and 0 rows
Run Code Online (Sandbox Code Playgroud)

看起来更智能的代码会创建一个虚假的列:

x <- list(NULL)
class(x) <- c("data.frame")
attr(x, "row.names") <- integer(0)
str(x)
#> 'data.frame':    0 obs. of  1 variable:
#>  $ : NULL
Run Code Online (Sandbox Code Playgroud)

有没有非黑客替代方案吗?

创建这样的东西的原因是满足一个可以处理空数据帧而不是NULL的函数.

这与类似的问题不同,因为它没有列也没有行.

r dataframe

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

将固定宽度的文本文件导入sqlite

将固定宽度的文本文件导入sqlite表的好方法是什么,最好不使用外围软件?

例如,指定每个字段的宽度

Field 1: 10 characters starting with the second character
Field 2: 5  characters starting with the twelfth character
Field 3: 7  characters starting with the eighteenth character
Run Code Online (Sandbox Code Playgroud)

这条线

AABCDEFGHIJABCDEAABCDEFGA
Run Code Online (Sandbox Code Playgroud)

将被导入为:

Field 1     Field 2  Field 3
ABCDEFGHIJ  ABCDE    ABCDEFG
Run Code Online (Sandbox Code Playgroud)

谢谢

sqlite text fixed-width

2
推荐指数
1
解决办法
2497
查看次数

将所有表描述到文本文件

如何描述数据库中的每个表并将所有结果导出到文本文件?例如

\o describe.txt
\d+ MY_TABLE
\o
Run Code Online (Sandbox Code Playgroud)

但对于每个表,每次都将输出附加到文本文件中。

postgresql

2
推荐指数
1
解决办法
4615
查看次数

列出同一文档末尾的knitr类型文档的完整代码

One chunk silently generates output.

```{r, echo = FALSE}
summary(cars)
```

How can the same chunk be automatically listed at the end like this?

```
summary(cars)
```
Run Code Online (Sandbox Code Playgroud)

knitr::purleval = FALSE如果它存在,那么参数就会起作用.

按名称列出单个块是理想的.

r sweave knitr r-markdown

2
推荐指数
1
解决办法
294
查看次数