小编Ind*_*til的帖子

熔化时保留变量级别的名称

有没有办法保留正在融化的变量的原始级别的名称?例如,在下面的示例中,有没有办法获得“alpha”、“beta”和“gamma”而不是“1”、“2”、“3”。

当然,我可以重命名它们,但我正在使用的数据集有大量级别,因此重命名它们将非常耗时且容易出错。

谢谢。

library(data.table)
#> Warning: package 'data.table' was built under R version 3.4.2
set.seed(2334)

# define the dataframe
df <-
  as.data.frame(
    cbind(
      a_alpha = rnorm(10),
      a_beta = rnorm(10),
      a_gamma = rnorm(10),
      b_alpha = rnorm(10),
      b_beta = rnorm(10),
      b_gamma = rnorm(10),
      id = c(1:10)
    )
  )

# check the structure of the wide format data
str(df)
#> 'data.frame':    10 obs. of  7 variables:
#>  $ a_alpha: num  -0.118 1.237 0.809 -0.766 -0.592 ...
#>  $ a_beta : num  0.0019 1.0639 …
Run Code Online (Sandbox Code Playgroud)

r reshape melt data.table

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

从默认更改 ggplot2::facet_wrap 标题

是否有任何可能的方法来更改 facet_wrap 变量的标签,如下所示。因此,例如,我希望它不是cyl: 4, cyl: 6, cyl: 8,而是读取condition: 4, condition: 6, condition: 8。当然,我可以通过重命名变量来做到这一点,但这不是我想要的。这是自定义函数的一个简单得多的版本,我不能随意重命名变量。

另一种说法是,我有任何自由标记facet_wrap我喜欢的任何东西吗?有点像在数据框 ( )中的x审美变量如何ggplot2可以有一些名称 (例如),但我仍然可以使用)将其替换为我自己的名称)。我想要类似的东西。 cylmtcarslabs(x = "cylinder")facet_wrap

library(dplyr)
library(datasets)
library(ggplot2)
data(mtcars)

# creating a dataframe
df <- dplyr::group_by(mtcars, .dots = c('cyl', 'am')) %>%
  dplyr::summarize(counts = n()) %>%
  dplyr::mutate(perc = (counts / sum(counts)) * 100) %>%
  dplyr::arrange(desc(perc))

# preparing the plot
ggplot2::ggplot(df, aes('', counts)) +
  geom_col(
    position = 'fill',
    color = …
Run Code Online (Sandbox Code Playgroud)

r data-visualization ggplot2

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

在r中使用atop函数时获得恒定的文本大小

下面是我编写的复杂自定义函数的一个简单得多的示例。在这个函数的全长形式中,

  • "layer1"对应于caption用户输入,
  • "layer2" 对应于统计测试的结果,并且
  • "layer3" 对应于有关执行的统计测试的详细信息。

但是当所有三层都包含在标题中时,它看起来像这样 -

library(ggplot2)

ggplot(iris, aes(Species, Sepal.Length)) +
  geom_boxplot()  +
  labs(caption = substitute(atop(substitute(
    atop("layer1", "layer2")
  )
  , "layer3")))
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.2.1)于 2018 年 11 月 9 日创建

所以我想找出一种方法可以使所有三层文本大小保持不变。我实际上不确定为什么在这种情况下文本大小会自动更改。

有什么办法可以防止这种情况发生吗?

r ggplot2 plotmath tidyverse

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

将 `rlang::exec` 与使用 `rlang::ensym` 的函数一起使用

我正在尝试编写一个更复杂的自定义函数,因此为了简单起见,我创建了一些玩具示例。

假设我想写一个函数——

  1. 自动决定要运行的适当函数:例如,t 检验或方差分析。
  2. 接受"quoted"unquoted参数

所以我编写了一个函数来运行 t 检验(按预期工作):

set.seed(123)
library(rlang)
library(tidyverse)

# t-test function
fun_t <- function(data, x, y) {
  # make sure both quoted and unquoted arguments work
  x <- rlang::ensym(x)
  y <- rlang::ensym(y)

  # t-test
  broom::tidy(stats::t.test(
    formula = rlang::new_formula({{ y }}, {{ x }}),
    data = data
  ))
}

# works fine
fun_t(mtcars, am, wt)
#> # A tibble: 1 x 10
#>   estimate estimate1 estimate2 statistic p.value parameter conf.low
#>      <dbl>     <dbl>     <dbl> …
Run Code Online (Sandbox Code Playgroud)

r function tidyverse rlang quosure

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

`pkgdown` 网站中的 `README` 徽章未正确呈现

到目前为止,我已经pkgdown为我的 R 包(例如ggstatsplotkittyR等)创建了 6 个不同的网站,但它们都没有出现以下问题。

第一次,运行以下命令

pkgdown::build_site(
  lazy = FALSE,
  run_dont_run = TRUE,
  seed = 123,
  devel = TRUE
)
Run Code Online (Sandbox Code Playgroud)

不呈现README徽章,我不知道为什么,因为yaml我使用的与我的其他软件包相似。我在GitHub repo上提出了问题pkgdown,但没有帮助。

更明确地说,这是我的README样子-

图片

这是pkgdown输出的样子-

  • pkgdown:https ://indrajeetpatil.github.io/ipmisc/

![图片](https://user-images.githubusercontent.com/11330453/72661380-76ada780-39d9-11ea-8897-b7e991deabb8.png)

在构建网站时,我没有收到任何警告或错误。但正如这里所见,主页上根本没有呈现徽章。关于为什么会发生这种情况或如何解决它的任何想法?

提前致谢。

r pkgdown

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

将函数调用转换为 R 中的字符

我有一个更复杂的函数,但它的最小版本归结为以下我需要将用户输入的函数转换为字符的地方。但我不知道如何做到这一点(我也尝试了一堆rlang不起作用的黑客)。

foo <- function(.f) {
  if (as.character(.f) == "stats::lm") {
    print("this is lm")
  } else {
    print("this is not lm")
  }
}

foo(stats::lm)
#> Error in as.character(.f): cannot coerce type 'closure' to vector of type 'character'
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

r function

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

使用带有 `dplyr::_join` 函数的 `rlang` quasiquotation

我正在尝试编写一个自定义函数,在其中使用rlang's quasiquotation。此函数也在内部使用dplyrjoin函数。我在下面提供了一个最小的工作示例来说明我的问题。

# needed libraries 
library(tidyverse)

# function definition
df_combiner <- function(data, x, group.by) {
  # check how many variables were entered for this grouping variable
  group.by <- as.list(rlang::quo_squash(rlang::enquo(group.by)))

  # based on number of arguments, select `group.by` in cases like `c(cyl)`,
  # the first list element after `quo_squash` will be `c` which we don't need,
  # but if we pass just `cyl`, there is no `c`, this will take care of that
  # issue …
Run Code Online (Sandbox Code Playgroud)

r dplyr rlang quasiquotes

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

在R路径中向后移动一个目录

假设这是我的R脚本所在的工作目录:

C:/Users/Indrajeet Patil/Dropbox/Study 1/Data analysis
Run Code Online (Sandbox Code Playgroud)

我知道如何在当前工作目录中创建新目录-

dir.create(path = paste(getwd(), '/Results', sep = '')) 
Run Code Online (Sandbox Code Playgroud)

我正在努力的是如何告诉R一个文件夹向后移动并创建一个新目录。因此,在此示例中,我想在文件夹内创建一个新文件夹Study 1并命名为Results

r

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

在 r 中使用 reprex 包创建可重现的示例,其中正在读取本地文件

我经常使用reprex::reprex创建可重现的R代码示例来获得其他人的帮助以消除代码中的错误。通常,我使用像irisor 这样的数据集创建最少的示例mtcars,并且效果很好。但reprex每当我需要使用自己数据时,我总是无法使用,因为问题非常具体,我不能依赖datasets库中的数据集。

在这种情况下,我收到以下错误:

# loading needed libraries
library(ggplot2)
library(cowplot)
library(devtools)

# reading the datafile
data <- utils::read.csv(file = "data.csv")
#> Warning in file(file, "rt"): cannot open file 'data.csv': No such file or
#> directory
#> Error in file(file, "rt"): cannot open the connection
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.2.0) 于 2018-02-19 创建。

在其他地方有一个关于前reprex时代的精彩讨论(如何制作一个伟大的 R 可重现示例?)。作者建议使用类似dput-

如果您有一些数据很难使用这些技巧构建,那么您始终可以使用例如 或索引来创建原始数据的子head()subset()。然后使用例如。给我们一些可以立即dput() …

r reproducible-research tidyverse reprex

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

使用带有构成功能的`purrr :: map`

我想知道如何使用purrr::map其中.f的两种不同功能的组成。

首先,让我们创建一个映射复合函数的列表:

library(tidyverse)

# create a list
x <- list(mtcars, tibble::as_tibble(iris), c("x", "y", "z"))

# extracting class of objects
purrr::map(.x = x, .f = class)
#> [[1]]
#> [1] "data.frame"
#> 
#> [[2]]
#> [1] "tbl_df"     "tbl"        "data.frame"
#> 
#> [[3]]
#> [1] "character"
Run Code Online (Sandbox Code Playgroud)

现在,让我们说,我要提取的第一个的元素class列表中的每个元素:

# this works but uses `map` twice
purrr::map(.x = x, .f = class) %>%
  purrr::map(.x = ., .f = `[[`, i = 1L)

#> [[1]]
#> [1] …
Run Code Online (Sandbox Code Playgroud)

functional-programming r purrr tidyverse

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