有没有办法保留正在融化的变量的原始级别的名称?例如,在下面的示例中,有没有办法获得“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) 是否有任何可能的方法来更改 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) 下面是我编写的复杂自定义函数的一个简单得多的示例。在这个函数的全长形式中,
"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 日创建
所以我想找出一种方法可以使所有三层的文本大小保持不变。我实际上不确定为什么在这种情况下文本大小会自动更改。
有什么办法可以防止这种情况发生吗?
我正在尝试编写一个更复杂的自定义函数,因此为了简单起见,我创建了一些玩具示例。
假设我想写一个函数——
"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) 到目前为止,我已经pkgdown为我的 R 包(例如ggstatsplot、kittyR等)创建了 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输出的样子-
在构建网站时,我没有收到任何警告或错误。但正如这里所见,主页上根本没有呈现徽章。关于为什么会发生这种情况或如何解决它的任何想法?
提前致谢。
我有一个更复杂的函数,但它的最小版本归结为以下我需要将用户输入的函数转换为字符的地方。但我不知道如何做到这一点(我也尝试了一堆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)
我怎样才能做到这一点?
我正在尝试编写一个自定义函数,在其中使用rlang's quasiquotation。此函数也在内部使用dplyr的join函数。我在下面提供了一个最小的工作示例来说明我的问题。
# 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脚本所在的工作目录:
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。
我经常使用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() …
我想知道如何使用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)