I've got a binary variable representing if event happened or not:
event <- c(0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0)
Run Code Online (Sandbox Code Playgroud)
I need to obtain a variable that would indicate the time when the last event happened. The expected output would be:
last_event <- c(0, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 13, 13, 13, 13)
Run Code Online (Sandbox Code Playgroud)
How can I obtain that with base R, tidyverse or any other …
我的目标是在条形图的末尾构建一个带有箭头的条形图。我去geom_segment
与arrow
定义。我想将一列映射到透明度上,但 alpha 美学似乎不适用于箭头对象。这是代码片段:
tibble(y = c(10, 20, 30), n = c(300, 100, 200), transparency = c(10, 2, 4)) %>%
ggplot() + geom_segment(aes(x = 0, xend = n, y = y, yend = y, alpha = transparency),
colour = 'red', size = 10, arrow = arrow(length = unit(1.5, 'cm'), type = 'closed')) +
scale_y_continuous(limits = c(5, 35))
Run Code Online (Sandbox Code Playgroud)
可以很容易地观察到arrow
对象在 的值较低时看起来不太好alpha
,显示其骨架而不是简单、透明的形状。有什么办法可以预防吗?
I need to write a function that would allow me to quickly do a dual axis plot using ggplot2
. I know that dual axis plots are generally deprecated, but still I think it may be useful if you're after observing similar patterns in time series (for all of those who disagree, please treat this question strictly technically). It is actually possible with sec_axis()
function from ggplot2
, but it needs a defined formula. So here's my attempt to calculate …
我最近开始使用Jupyter Lab,但我的问题是我要处理非常大的数据集(通常,数据集本身大约是计算机RAM的1/4)。经过几次转换后,另存为新的Python对象,我倾向于耗尽内存。问题是,当我接近可用的RAM限制并执行需要另一个RAM空间的任何操作时,计算机将冻结,而修复它的唯一方法是重新启动它。这是Jupyter Lab / Notebook中的默认行为,还是我应该设置的某些设置?通常,我希望程序崩溃(例如在RStudio中),而不是整个计算机
我有一个像下面这样的小标题:
structure(list(id = 1:11, var1 = c("A", "C", "B", "B", "B", "A",
"B", "C", "C", "C", "B"), var2 = list(NULL, NULL, NULL, structure(list(
x = c(0, 1, 23, 3), y = c(0.75149005651474, 0.149892757181078,
0.695984086720273, 0.0247649133671075)), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame")), NULL, NULL,
NULL, NULL, NULL, NULL, NULL)), row.names = c(NA, -11L), class = c("tbl_df",
"tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)
我只想留下var2
非空的行。但简单!is.null()
就是行不通。df %>% filter(!is.null(var2))
返回整个df
. 为什么会这样,我如何过滤掉所有列NULL
中的var2
行?
我有一堆R脚本可以读取各种来源的数据,执行所需的所有分析并进行一些可视化.现在,我需要将可视化部分传递给Tableau,并在后者中准备整个仪表板.
从我知道我可以全部保存data.frames
到.Rdata
文件,并在加载的Tableau他们,但似乎从长远来看,要cumbersone,而整个程序应尽可能自动化.
我要知道的第二个选项是读取Tableau中的数据,连接到R via Rserve
并从中调用简单的R脚本Tableau
.这是一个选择,但是:
所以我的问题是集成R和Tableau的最佳方法是什么,以便在我执行RI中的所有分析之后只能用几个图更新早期Tableau仪表板中的数据?
我有以下列表:
> list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
[[1]]
[1] 3 4 5 8
[[2]]
[1] 2 6 9 10
[[3]]
[1] 1 7
Run Code Online (Sandbox Code Playgroud)
所以我们可以说3属于组1,6属于组2,7属于组3,依此类推.我需要一个反向映射,即对于我希望拥有它所属的组ID的每个数字(参见预期输出):
> list(3, 2, 1, 1, 1, 2, 3, 1, 2, 2)
[[1]]
[1] 3
[[2]]
[1] 2
[[3]]
[1] 1
[[4]]
[1] 1
[[5]]
[1] 1
[[6]]
[1] 2
[[7]]
[1] 3
[[8]]
[1] 1
[[9]]
[1] 2
[[10]]
[1] 2
Run Code Online (Sandbox Code Playgroud)
我认为purrr::transpose
应该做的工作,但它并不完全符合我的意图,是吗?怎么做到呢?
PS.最终,我只需要一个形式的矢量:3 2 1 1 1 2 …
structure(list(group = c(17L, 17L, 17L, 18L, 18L, 18L, 18L, 19L,
19L, 19L, 20L, 20L, 20L, 21L, 21L, 22L, 23L, 24L, 25L, 25L, 25L,
26L, 27L, 27L, 27L, 28L), var = c(74L, 49L, 1L, 74L, 1L, 49L,
61L, 49L, 1L, 5L, 5L, 1L, 44L, 44L, 12L, 13L, 5L, 5L, 1L, 1L,
4L, 4L, 1L, 1L, 1L, 49L), first = c(0, 0, 1, 0, 1, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用拟合随机森林模型caret
。我的训练数据重量为129MB,我正在具有8核和52GB RAM的Google Cloud上进行计算。我正在使用的代码如下:
library(caret)
library(doParallel)
cl <- makeCluster(3, outfile = '')
registerDoParallel(cl)
model <- train(x = as.matrix(X_train),
y = y_train,
method = 'rf',
verbose = TRUE,
trControl = trainControl(method = 'oob',
verboseIter = TRUE,
allowParallel = TRUE),
tuneGrid = expand.grid(mtry = c(2:10, 12, 14, 16, 20)),
num.tree = 100,
metric = 'Accuracy',
performance = 1)
stopCluster(cl)
Run Code Online (Sandbox Code Playgroud)
尽管有8个内核,但任何尝试使用3个以上内核的makeCluster
结果都会导致以下错误:
unserialize(socklist [[n]])中的错误:从连接读取错误
因此,我认为也许内存分配存在问题,并且仅使用3个内核进行了尝试。经过数小时的培训,当我期望获得结果时,令我惊讶的是,唯一的错误是以下错误:
错误:无法分配大小为1.9 Gb的向量
尽管如此,我的Google云端实例仍具有52GB的内存,因此我决定检查其中有多少是当前可用的。
as.numeric(system("awk '/MemFree/ {print $2}' /proc/meminfo", intern=TRUE))
Run Code Online (Sandbox Code Playgroud)
[1] 5606656
大于47GB。因此,假设在培训结束时无法分配2GB,则似乎随机培训森林使用了45GB以上。我知道我的训练数据集被引导100次以生成一个随机森林,因此100份训练数据副本的重量约为13GB。同时,我的总RAM分为3个群集,这给了我39GB的内存。它应该给我留下约6GB的内存,但显然没有。不过,这仍然是假设在构建分离的树之后没有释放任何内存,我怀疑情况是否如此。
因此,我的问题是:
library(tidyverse)
library(lubridate)
date <- seq(ymd('2018-08-01'), ymd('2018-08-31'), by = '1 day')
c <- 21.30
x1 <- runif(length(date), 0, 20)
x2 <- rnorm(length(date), 10, 3)
x3 <- abs(rnorm(length(date), 40, 10))
data <- data.frame(c, x1, x2, x3) %>%
t() %>% as.data.frame() %>% rownames_to_column('var')
data <- data %>%
mutate(category1 = c('catA', 'catB', 'catB', 'catC') %>% as.factor(),
category2 = c('catAA', 'catBA', 'catBB', 'catCA') %>% as.factor())
names(data) <- c('var', as.character(date), 'category1', 'category2')
data_long <- data %>%
gather(date, value, -var, -category1, -category2) %>%
mutate(date = ymd(date))
data_long …
Run Code Online (Sandbox Code Playgroud)