我想将data.table cols的子集转换为新类.这里有一个流行的问题(在data.table中转换列类)但是答案创建了一个新对象,而不是在starter对象上运行.
举个例子:
dat <- data.frame(ID=c(rep("A", 5), rep("B",5)), Quarter=c(1:5, 1:5), value=rnorm(10))
cols <- c('ID', 'Quarter')
Run Code Online (Sandbox Code Playgroud)
如何最好地将cols列转换为(例如)一个因子?在普通的data.frame中,你可以这样做:
dat[, cols] <- lapply(dat[, cols], factor)
Run Code Online (Sandbox Code Playgroud)
但这对data.table不起作用,也不适用
dat[, .SD := lapply(.SD, factor), .SDcols = cols]
Run Code Online (Sandbox Code Playgroud)
来自Matt Dowle(2013年12月)的链接问题中的评论表明以下情况很好,但看起来不那么优雅.
for (j in cols) set(dat, j = j, value = factor(dat[[j]]))
Run Code Online (Sandbox Code Playgroud)
目前是否有更好的data.table答案(即更短+不生成计数器变量),或者我应该只使用上面的+ rm(j)?
table基本R中的函数添加了漂亮的行/列标签,但是当我使用knitr::kable它们时就会消失.保持这些的任何简单方法,除了在html/markdown方面添加它们?
可重复的例子:
library(knitr)
# reproducibility
set.seed(123)
# here's a df
some_data <-
data.frame(a=sample(c('up','down'), 10, replace=T),
b=sample(c('big','small'), 10, replace=T))
# using table() you get nice labels ('a' and 'b', in this case)
table(some_data)
Run Code Online (Sandbox Code Playgroud)

# that goes away with kable, in either markdown or html format (I care about html)
kable(table(some_data))
kable(table(some_data), format='html')
Run Code Online (Sandbox Code Playgroud)

使用geom_point与position_jitterdodge作品只有当你设置填充美感.我不明白为什么会这样!
这个命令
library(ggplot2)
ggplot(diamonds[ sample(nrow(diamonds), 1000), ],
aes(x = cut, y = carat, color = clarity)) +
geom_point(shape = 21, position = position_jitterdodge())
Run Code Online (Sandbox Code Playgroud)
产生错误:
Error: position_jitterdodge requires the following missing aesthetics: fill
Run Code Online (Sandbox Code Playgroud)
但这有效:
ggplot(diamonds[ sample(nrow(diamonds), 1000), ],
aes(x = cut, y = carat, fill = clarity)) +
geom_point(shape = 21, position = position_jitterdodge())
Run Code Online (Sandbox Code Playgroud)

简单地提供NA值来填充不是一个可行的解决方法:
ggplot(diamonds[ sample(nrow(diamonds), 1000), ],
aes(x = cut, y = carat, color = clarity, fill=NA)) +
geom_point(shape = 21, position = position_jitterdodge())
> …Run Code Online (Sandbox Code Playgroud) 假设我有一个data.frame,其中包含几列分类数据和一列定量数据.这是一个例子:
my_data <- structure(list(A = c("f", "f", "f", "f", "t", "t", "t", "t"),
B = c("t", "t", "t", "t", "f", "f", "f", "f"),
C = c("f","f", "t", "t", "f", "f", "t", "t"),
D = c("f", "t", "f", "t", "f", "t", "f", "t")),
.Names = c("A", "B", "C", "D"),
row.names = 1:8, class = "data.frame")
my_data$quantity <- 1:8
Run Code Online (Sandbox Code Playgroud)
现在my_data看起来像这样:
A B C D quantity
1 f t f f 1
2 f t f t 2
3 f t …Run Code Online (Sandbox Code Playgroud) knitr将.Rmd编织为.html时会自动生成链接。通常这很有用,但现在不是我想要的。
假设您有以下.Rmd文件:
---
title: "Doc title"
output: html_document
---
I don't want this@to-be-a-link.com, but it comes out that way.
If you wrap it in an R expression `r "this@is-still-a-link.com"`.
Is there some kind of CSS trick I can avail myself of if I wanted <style='nolink'>www.something.com</style> not to be a link?
Run Code Online (Sandbox Code Playgroud)
像这样编织:
library(knitr)
knit2html('that_file.Rmd', 'that_file.html')
Run Code Online (Sandbox Code Playgroud)
导致所有这些东西都是链接。

是否有一种简单的方法通常可以保留自动链接生成,但是在特定的行中有选择地禁用它?感谢您的任何想法。
编辑:我想我应该在接受之前实际尝试过以下解决方案。这在.Rmd
I don't want this <!-- breaklink -->@to-be-a-link.com
Run Code Online (Sandbox Code Playgroud)
...实际上并没有解析为HTML注释,因为-变成了破折号(由knitr?pandoc?),然后我得到:

关于如何从 docker 容器内部寻址本地文件夹,有很多很少有人投票的问题,但我找不到一个与我的完全匹配的问题,所以这里有另一个:
如何运行 docker 容器并挂载本地文件夹,以便 R/RStudio 可以在容器内访问它?
这听起来有点像:在 Rstudio docker 中挂载本地主目录?并使用类似的方法,我可以启动一个容器并安装一个卷:
docker run -d -p 8787:8787 -v $HOME/my_folder:/LOOKATMEEE -e ROOT=TRUE rocker/tidyverse:3.4
Run Code Online (Sandbox Code Playgroud)
如果我在容器中运行 bash shell,我可以看到文件夹:
docker exec -it 38b2d6ca427f bash
> ls
bin dev home lib LOOKATMEEE mnt proc run srv tmp var boot etc init lib64 media opt root sbin sys usr
# ^ there is is!
Run Code Online (Sandbox Code Playgroud)
但是,如果我在 连接到 RStudio 服务器localhost:8787,则在文件窗格中看不到它,list.files()在 R 控制台中运行时也看不到它:
我确定我错过了一些基本的东西,但如果有人能告诉我那是什么......谢谢!
我有一个函数返回一个data.table附加了各种有用的用户定义属性.但是,我注意到,当操纵data.table时,属性会消失.
library(data.table)
my_dt <- data.table(col1 = rnorm(20), col2 = letters[1:20])
# store some user attribute
attr(my_dt, 'title') <- 'This is my data.table'
# now it's there
attributes(my_dt)
# but here it's gone
attributes(my_dt[order(col1)])
Run Code Online (Sandbox Code Playgroud)
有没有办法让data.table'persist'的属性适用于上述情况(除了将它们存储在一个单独的对象中)?
似乎属性确实存在于常规 data.frames
my_df <- data.frame(col1 = rnorm(20), col2 = letters[1:20])
# store some user attribute
attr(my_df, 'title') <- 'This is my data.frame'
# there it is
attributes(my_df)
# still there
attributes(my_df[order(my_df$col1), ])
Run Code Online (Sandbox Code Playgroud) 这是一个基本情节:
ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
geom_point() +
theme_linedraw()
Run Code Online (Sandbox Code Playgroud)
如果我想要不旋转y轴标签,我可以这样做:
ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
geom_point() +
theme_linedraw() +
theme(axis.title.y =
element_text(color='red', angle=0, vjust = 0.5))
Run Code Online (Sandbox Code Playgroud)
如果我想要它在右侧,我可以这样做:
ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
geom_point() +
theme_linedraw() +
scale_y_continuous(position = 'right')
Run Code Online (Sandbox Code Playgroud)
但为什么我不能两个都做?
ggplot(data.frame(x=1:10,y=1:10), aes(x,y)) +
geom_point() +
theme_linedraw() +
scale_y_continuous(position = 'right') +
theme(axis.title.y =
element_text(color='red', angle=0, vjust = 0.5))
Run Code Online (Sandbox Code Playgroud)
以供参考:
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X El Capitan 10.11.6
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils …Run Code Online (Sandbox Code Playgroud) 如何才能使unicode字符出现在ggplot图表的构面标签中(或实际上,在任何地方)?
周围有很多相关的帖子,但是没有一个对我有用。(我指出了为什么最后没有重复项)
因此,给出以下代码:
library(ggplot2)
facets <- c('?', '?')
facets2 <- c('\u2713', '\u2717')
facets3 <- c('check', 'x')
set.seed(123)
my_df <- data.frame(x = runif(40), y = runif(40),
z = rep(facets, each=20),
stringsAsFactors = F)
ggplot(my_df, aes(x, y, color=z)) + geom_point() +
facet_wrap(~z) +
theme(legend.position = 'none')
Run Code Online (Sandbox Code Playgroud)
...我得到此图(注意缺少的构面标签):
当我使用facets2标签时(即指定转义的字符代码而不是文字),我会得到相同的结果,但是当我使用facets3所有标签时,当然会出现应有的结果。
我的 sessionInfo()
我正在使用R Studio 1.0.136,我sessionInfo()是
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics …Run Code Online (Sandbox Code Playgroud) 我想在绘图区域内放置标签。
这是一个示例图:
library(ggplot2)
set.seed(123)
random_word <- function() paste(sample(letters, 10, replace = T), collapse='')
dat <- data.frame(x = replicate(4, random_word()),
y = runif(5*4, 0, 100))
ggplot(dat, aes(x = x, y = y)) +
geom_point() +
coord_flip() +
theme_minimal()
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过geom_text破解来获得我想要的结果:
ggplot(dat, aes(x = x, y = y)) +
geom_point() +
geom_text(data = dat[1:4,],
aes(label=x),
y = 1,
hjust=0, vjust=-1 ) +
coord_flip() +
theme_minimal() +
theme(axis.text.y = element_blank())
Run Code Online (Sandbox Code Playgroud)
对这个问题的评论 [ move axis labels ggplot ] 表明这margin是在当前 ggplot 中执行此操作的非黑客方法,但它似乎没有在绘图区域内移动轴文本。 …
浏览某些数据时遇到了这种情况,感觉就像是意外行为,所以以为我会发布。
geom_jitter接受高度/宽度参数来确定抖动的宽度,默认值为40%。当您添加对数刻度时,这40%似乎适用于原始值。但是,如果要调整此参数,则在对数转换后应用该值。
可以这样说明:
library(ggplot2)
library(patchwork)
set.seed(1)
dat <- data.frame(x=round(rlnorm(2000)), y=round(rlnorm(2000)))
# THESE TWO PLOTS ARE THE SAME
# with jitter
p1 <- ggplot(dat, aes(x,y)) + geom_jitter(alpha=.1) +
labs(title='regular scale, jitter with default height/width',
subtitle = '')
# with jitter, and explicit (but same as default) jitter size
p2 <- ggplot(dat, aes(x,y)) + geom_jitter(alpha=.1, height=.4, width=.4) +
labs(title='regular scale, jitter with 40% height/width',
subtitle = '<== same as that')
# THESE TWO PLOTS ARE NOT THE SAME
# with jitter …Run Code Online (Sandbox Code Playgroud) r ×11
ggplot2 ×5
data.table ×3
knitr ×2
markdown ×2
attributes ×1
docker ×1
pandoc ×1
persistent ×1
reshape2 ×1
rstudio ×1
unicode ×1
user-defined ×1
xtable ×1