天道酬勤,学无止境

r

How to convert `c(a,b)` to parameter of `table(a,b)`?

I need to write function vec_to_para,when I run vec_to_para(c('vs','gear'),mtcars),should output result of with(mtcars,table(vs,gear)). I asked a similar quesiton yesterday,then I tried paste() #expected result > with(mtcars,table(vs,gear)) gear vs 3 4 5 0 12 2 4 1 3 10 1 #my function,where is the problem? >vec_to_para<-function(vec,df){ with(df,tables(paste(vec,collapse=','))) } #run to test function >vec_to_para(c('vs','gear'),mtcars) vs,gear 1 Where is the problem of vec_to_para function?

2022-01-25 05:39:25    分类:问答    r

R - (ggplot) Make geom_step jumps dashed

I'm plotting a discrete CDF. I have a few questions regarding geom_step which I'm not finding by using Google. Is it possible to make the line segment representing the jump dashed rather than solid to better show whats going on? Is it possible to add geom_point more efficiently than I do? (less c/p). Below is my current solution: library(tidyverse) library(ggthemes) theme_set(theme_few()) x0 <- seq(-0.5, -0.01, by = 0.01) x1 <- seq(0, 0.99, by = 0.02) x2 <- seq(1, 1.99, by = 0.02) x3 <- seq(2, 2.99, by = 0.02) x35 <- seq(3, 3.49, by = 0.01) x4 <- seq(3.5, 3.99, by = 0.01) tibble_ex <- tibble(

2022-01-25 05:38:48    分类:问答    r   ggplot2

Get size of plot in pixels in R

I am generating a heatmap in R with lots of rows. TL;DR how do I get the real size of a plot in R? df=data.frame(one=1:100,two=101:200,three=201:300) names=1:100 names=paste0("Cell",names) rownames(df)=(names) pheatmap(df,scale="row") default image fits in window, but we can't read row names. pheatmap(df,scale="row",cellheight = 10) changing the cell height lets us read row names but now the image doesn't fit in the window! In this example i am using pheatmap, but also run into this stuff with other plot generating packages. While I've grown to expect frustrating behavior like this from R and

2022-01-25 05:38:32    分类:问答    r   plot   pheatmap

Cumulative aggregates within tidyverse

say I have a tibble (or data.table) which consists of two columns: a <- tibble(id = rep(c("A", "B"), each = 6), val = c(1, 0, 0, 1 ,0,1,0,0,0,1,1,1)) Furthermore I have a function called myfun which takes a numeric vector of arbitrary length as input and returns a single number. For example, you can think of myfun as being the standard deviation. Now I would like to create a third column to my tibble (called result) which contains the outputs of myfun applied to val cumulated and grouped with respect to id. For example, the first entry of result should contain mfun(val[1]). The second entry

2022-01-25 05:28:32    分类:问答    r   dataframe   dplyr   tidyverse   purrr

在 RStudio 中安装 RMarkdown 包在 Windows 7 上不起作用(Installing RMarkdown packages in RStudio does not work on Windows 7)

问题 我已经在 Windows 7 64 位上安装了 RStudio 0.98,当我第一次尝试创建新的 RMarkdown 脚本时,它要求我安装 RMarkdown(包或其他东西)。 当我确认这一点时,该过程将失败,并引发以下错误: Installing package into 'C:/Users/grosseti/AppData/Roaming/R' (as 'lib' is unspecified) Der Befehl "C:\Program" ist entweder falsch geschrieben oder konnte nicht gefunden werden. Warning messages: 1: running command '"C:/Program Files/R/R-3.1.2/bin/x64/R" CMD INSTALL -l "C:\Users\grosseti\AppData\Roaming\R" "C:/Program Files/RStudio/R/packages/rmarkdown_0.3.10_b96214b9ac86b437067a0aa21442203f52face83.tar.gz"' had status 1 2: In utils::install.packages("C:/Program Files/RStudio/R

2022-01-25 05:27:44    分类:技术分享    r   rstudio   r-markdown

A way to not hardcode items in css with R Shiny app?

I have the below code example (taken from here) . Is there a way for the below to work without hardcoding the values "a" and "b" in the html css? If I have hundreds of items I don't want to have to hardcode each one in the call to .option[data-value=a] etc. shinyApp( ui = shinyUI(fluidPage( tags$head( tags$style(HTML(" .option[data-value=a], .item[data-value=a]{ background: red !important; color: white !important; } .option[data-value=b], .item[data-value=b]{ background: green !important; color: white !important; } ")) ), sidebarLayout( sidebarPanel( selectizeInput("select", label=NULL

2022-01-25 05:27:40    分类:问答    css   r   shiny   shinyapps

Add formula terms programmatically

I am running a simulation and need to test the performance of different sized formulas. Drawing from data frame with columns V1, V2, V3... Vk, I need to programmatically create formulas like fit <- ols(y ~ V1 + V2 + V3, data=dataframe) and so forth. How can I code the formula to be able to scale the length of the formula?

2022-01-25 05:27:23    分类:问答    r   regression

如何将 95% 的置信区间添加到 ggplot 中因子水平的比例图?(How to add 95% confidence intervals to graph of proportions of factor levels in ggplot?)

问题 我想在之前提出的问题得到的很好答案的基础上再接再厉: 图形比例在因子水平而不是 ggplot2 中的计数 我希望以代码为基础: var1 <- c("Left", "Right", NA, "Left", "Right", "Right", "Right", "Left", "Left", "Right", "Left", "Left","Left", "Right", "Left", "Right", "Right", "Right", "Left", "Left", "Right", NA, "Left", "Left","Left", "Right", NA, "Left", "Right", "Right", "Right", "Left", "Left", "Right", "Left", "Left","Left", "Right", "Left", "Right", "Right", "Right", "Left", "Left", "Right", NA, "Left", "Left") var2 <- c("Higher", "Lower", NA, "Slightly higher", "Slightly higher", "Slightly higher", "Lower", "Slightly higher", "Higher", "Higher",

2022-01-25 05:20:49    分类:技术分享    r   ggplot2   errorbar

在管道 %>% 之后插入 if 语句以在自定义函数中返回错误消息(insert if statement after pipe %>% to return error message within custom function)

问题 非常简单的问题,但找不到解决方案,如果变量不属于dplyr管道中的某个class ,我想做的就是从函数返回错误消息。 可以说我有: library(tidyverse) library(scales) library(ggplot2) data1 <- data.frame(date1 = sample(seq(as.Date("2005-01-01"), as.Date('2018-01-01'), by="day"), 5), num1 = 1:5) data1 date1 num1 1 2008-10-20 1 2 2005-01-17 2 3 2014-03-19 3 4 2005-01-24 4 5 2014-01-21 5 和一个绘制日期直方图的小函数: hist_date_fun <- function(df, date_varaible) { date_varaible = enquo(date_varaible) df %>% group_by(month = floor_date(!!date_varaible, "month")) %>% dplyr::summarize(freq = n()) %>% ggplot(aes(x = month, y = freq)) + geom_bar(stat="identity") + scale_x_date

2022-01-25 05:19:23    分类:技术分享    r   function   if-statement   dplyr   return

如何按组计算日期之间的时间差(how to calculate time difference between dates by group)

问题 我有一个包含 date.times 和位置的数据框。 我想计算组内记录与前一个记录(按日期排列)之间的分钟差,并变异为新列。 我已经想出了如何使用循环来执行此操作,但这仅适用于所有组(位置),我不确定如何按组执行此操作。 # fake data set for example: df <- data.frame( location = c( 1,1,3,4,4,5,6,5,4,4,3,2,2,1,1,2,3,4,4,2 ), date.time = c( "2017-10-22 04:49:23", "2017-10-23 01:02:06", "2017-10-23 01:09:17", "2017-10-23 18:32:46", "2017-10-24 18:50:19", "2017-11-01 03:07:24", "2017-11-01 19:05:58", "2017-11-02 01:56:48", "2017-11-02 01:58:16", "2017-11-02 02:00:38", "2017-11-06 19:53:56", "2017-11-09 13:08:39", "2017-09-18 01:25:27", "2017-09-19 05:19:43", "2017-09-21 21:42:33", "2017-09-22 00:49:16"

2022-01-25 05:18:02    分类:技术分享    r   for-loop   dplyr   purrr   difftime