是否有更简洁的方法将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)
但它似乎有点笨拙.
默认情况下不适合幻灯片,甚至不通过任何其他方式打印.
这是.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) 给定POSIXct日期时间,如何提取当月的第一天进行聚合?
library(lubridate)
full.date <- ymd_hms("2013-01-01 00:00:21")
Run Code Online (Sandbox Code Playgroud) 很多人都会问如何剥夺时间并保留日期,但另一方面呢?鉴于:
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",而我的尝试只是在运行时使用了当前日期.
我有三个文本文档存储为名为"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) 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) 这就是我做的:
# 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) marrangeGrob在gridExtra整理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) 这是一个没有行而没有列的空数据框架:
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的函数.
这与类似的问题不同,因为它没有列也没有行.
将固定宽度的文本文件导入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)
谢谢
如何描述数据库中的每个表并将所有结果导出到文本文件?例如
\o describe.txt
\d+ MY_TABLE
\o
Run Code Online (Sandbox Code Playgroud)
但对于每个表,每次都将输出附加到文本文件中。
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 ×9
dplyr ×2
knitr ×2
r-markdown ×2
beamer ×1
build ×1
collect ×1
dataframe ×1
datetime ×1
fixed-width ×1
gridextra ×1
linux-mint ×1
lua ×1
lubridate ×1
pandoc ×1
postgresql ×1
sqlite ×1
sweave ×1
text ×1
vim ×1