我正在编写一个使用 tidyeval 的包。因为我使用 tidyeval 我在描述文件的导入下列出了 rlang。
其中一个函数包含几行使用 :=
像这样:
data %>%
dplyr::mutate(
!!New_R := AP_R_X*!!X + AP_R_Y*!!Y + AP_R_Z*!!Z,
!!New_U := AP_U_X*!!X + AP_U_Y*!!Y + AP_U_Z*!!Z,
!!New_F := AP_F_X*!!X + AP_F_Y*!!Y + AP_F_Z*!!Z)
Run Code Online (Sandbox Code Playgroud)
该代码按预期工作,但在运行时收到以下注释 devtools::check()
no visible global function definition for ':='
Run Code Online (Sandbox Code Playgroud)
我怎样才能摆脱这个笔记?这不是 rlang 评估的一部分吗?
编辑:我已经阅读了问题“没有可见的 'median' 全局函数定义,尽管那里的答案解释了为什么会出现这样的问题。它没有解释为什么:=在我导入时没有定义rlang。我已经编辑了这个问题来制作这个更清楚。
我正在构建一个使用 {{ }} (卷曲卷曲或双胡子)的函数
我希望用户能够将多个变量传递到同一个 {{ }},但我不确定使用 {{ }} 是否可行。我找不到任何显示如何执行此操作的示例。
如果可能的话,你能告诉我吗,如果可能,请帮助我使下面的最小 reprex 工作?
library(tidyverse)
group_mean <- function(.data, group){
.data %>%
group_by({{group}}) %>%
summarise_all(mean)
}
# Works
mtcars %>%
group_mean(group = cyl)
# Fails
mtcars %>%
group_mean(group = c(cyl, am))
Error: Column `c(cyl, am)` must be length 32 (the number of rows) or one, not 64
Run Code Online (Sandbox Code Playgroud) 我发现在使用purrr和某些map()变体时很难调试我的代码。特别是我在定位代码失败的位置时遇到问题,因为错误消息没有告诉我哪一行(数据帧)引发了错误。
使用时定位错误的好方法是什么purrr?
考虑以下示例:
library(tidyverse)
# Prepare some data
set.seed(1)
a <- tibble(
x = rnorm(2),
y = rnorm(2))
b <- tibble(
x = rnorm(2),
y = rnorm(2))
c <- tibble(
x = rnorm(2),
y = letters[1:2])
df <- tibble(
dataframes = list(a,b,c))
df
#> # A tibble: 3 x 1
#> dataframes
#> <list>
#> 1 <tibble [2 x 2]>
#> 2 <tibble [2 x 2]>
#> 3 <tibble [2 x 2]>
# A …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种从每个组中提取第一个和最后一个非NA值的方法.我正在使用dplyr :: first()和dplyr :: last(),但我不知道如何选择第一个或最后一个非NA值.
library(dplyr)
set.seed(123)
d <- data.frame(
group = rep(1:3, each = 3),
year = rep(seq(2000,2002,1),3),
value = sample(1:9, r = T))
#Introduce NA values in first row of group 2 and last row of group 3
d %>%
mutate(
value = case_when(
group == 2 & year ==2000 ~ NA_integer_,
group == 3 & year ==2002 ~ NA_integer_,
TRUE ~ value))%>%
group_by(group) %>%
mutate(
first = dplyr::first(value),
last = dplyr::last(value))
Run Code Online (Sandbox Code Playgroud)
结果(有问题)
# A tibble: 9 x …Run Code Online (Sandbox Code Playgroud) 我想并排或至少在同一个文档中显示由 gganimate 包制作的动画。
使用的绘图:
library(ggplot2)
library(gganimate)
anime_one <-
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
# Here comes the gganimate code
transition_states(
gear,
transition_length = 2,
state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')
anime_two <-
ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(cyl))) +
geom_boxplot() +
# Here comes the gganimate code
transition_states(
gear,
transition_length = 2,
state_length = 1
) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')
Run Code Online (Sandbox Code Playgroud)
第一次尝试:
library(patchwork)
anime_one + anime_two
Error in UseMethod("ggplot_gtable") :
no applicable method for …Run Code Online (Sandbox Code Playgroud) 自从更新到tidyr版本 1.0.0 以来,我在取消嵌套数据帧列表时开始出现错误。
出现错误的原因是列表中的某些数据帧包含具有所有 NA 值(逻辑)的列,而其他数据帧包含相同的列但具有一些字符值(字符)。具有所有 NA 值的列被编码为逻辑值,而其他列被编码为字符向量。
早期版本的默认行为tidyr处理不同的列类型没有问题(至少我在运行脚本时没有收到此错误)。
我可以从内部解决这个问题tidyr::unest()吗?
可重现的例子:
library(tidyr)
a <- tibble(
value = rnorm(3),
char_vec = c(NA, "A", NA))
b <- tibble(
value = rnorm(2),
char_vec = c(NA, "B"))
c <- tibble(
value = rnorm(3),
char_vec = c(NA, NA, NA))
tibble(
file = list(a, b, c)) %>%
unnest(cols = c(file))
#> No common type for `..1$file$char_vec` <character> and `..3$file$char_vec`
#> <logical>.
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.3.0)于 2019-10-11 创建
我正在构建一个显示介于 0.0 和 1.0 之间的值的克利夫兰点图。
我接近拥有我想要的情节,但一个小细节困扰着我。因为我使用 expand = c(0,0) 截断了 x 轴上 1.00 中的最后一个 0。
我曾尝试更改各种设置,但没有运气。
这个问题有一个类似的标题这个职位呢,遗憾的是,没有帮助。
你能帮我保持图的边界而不切断 x 轴上 1.00 的最后一个零吗?
代表:
library(tidyverse)
df <- tibble(
Tastyness = c(0.6, 0.7, 0.9, 0.95, 0.98),
Fruit = c("Bananas", "Apples", "Oranges", "Mango", "Peach")
)
ggplot(df, aes(x = Tastyness, y = Fruit)) +
geom_point(size = 4) +
theme_bw() +
scale_x_continuous(
limits = c(0.0, 1.0),
expand = c(0, 0),
breaks = c(0, 0.5, 0.75, 0.9, 1.00)
)
Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个包含 unicode 字符的图(加减号U +00B1)
以下是一些虚假数据:
library(tidyverse)
set.seed(1)
df <- crossing(
Rated_Movement = c("Running", "Jumping"),
Rater = c("John Snow", "Batman", "Hulk")) %>%
mutate(
Error = runif(n = 6, min = 0, max=2))
Run Code Online (Sandbox Code Playgroud)
这是到目前为止我的情节代码:
ggplot(df, aes(x = Rated_Movement, y = Rater, fill = Error)) +
geom_tile(color = "black", size = 0.5, alpha = 0.8)+
geom_text(aes(label = paste("+-", round(Error,2))))+
scale_fill_gradientn(colours = terrain.colors(10))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
Run Code Online (Sandbox Code Playgroud)
有没有办法可以将 +- 更改为 unicode 字符 U+00B1
我尝试使用反斜杠来转义:paste("\U+00B1", round(Error,2)) 但出现错误:“错误:'\U' 在以“”\U”开头的字符串中没有使用十六进制数字”
任何帮助都感激不尽。此致
我正在尝试构建一个收集(pivot_longer)数据的函数。
在大多数情况下,该函数应收集所提供数据中除一列之外的所有列,但应允许用户在所提供的数据中指定不应收集的其他列。这是用户很少会做的事情,所以参数应该默认为没有额外的列。
我认为我的函数失败了,因为它默认为NULL.
数据:
library(tidyverse)
sample_data <- tibble(
frame = rep(seq(1:20), 2),
ID = rep(c(1,2), each = 20),
a = rnorm(n = 40),
b = rnorm(n = 40),
c = rnorm(n = 40))
Run Code Online (Sandbox Code Playgroud)
功能:
a_gather_function <- function(.data, also_dont_gather = NULL) {
.data %>%
tidyr::gather(key, value, -frame, -{{also_dont_gather}})
}
Run Code Online (Sandbox Code Playgroud)
如果我为参数提供一列,则该函数有效 also_dont_gather
sample_data %>%
a_gather_function(also_dont_gather = ID) %>%
head(5)
# A tibble: 5 x 4
frame ID key value
<int> <dbl> <chr> <dbl>
1 1 1 a -0.626
2 2 …Run Code Online (Sandbox Code Playgroud) 我想使用 ggplot2 绘制两条线之间的角度,这意味着类似于下图中的粗体红线。有没有简单的解决方案?
制作没有红线的图的数据和代码:
library(tidyverse)
df <- tibble(
line = c("A", "A", "B", "B"),
x = c(1, 5, 1, 3),
y = c(1, 3, 1, 5))
ggplot(
df, aes(x, y, group = line))+
geom_path()
Run Code Online (Sandbox Code Playgroud) 我正在尝试计算两组经度和纬度坐标之间的距离。
我正在使用 geosphere 包中的函数 distm() 来执行此操作。
如果我手动将值放入 distm() 函数中,它工作正常,但我无法让它在我的 mutate 命令中工作。
在 mutate 函数中运行它时,出现错误:
Error in mutate_impl(.data, dots) :
Evaluation error: Wrong length for a vector, should be 2.
Run Code Online (Sandbox Code Playgroud)
@Dotpi 在评论中写道:“一个小笔记。方法 geosphere:distm 没有被矢量化。要对其进行矢量化,请使用 apply 函数。” 当他在此线程中回复时(使用 R 计算两点(纬度,经度)之间的地理空间距离的函数)
由此我猜测这就是导致 mutate 函数出错的原因,但我不知道如何解决。我更喜欢 tidyverse 解决方案,但任何帮助表示赞赏。
下面是一个测试数据框,首先是产生错误的代码,然后是一个工作示例,我在 DF 中手动插入第一行的值。
library(tidyverse)
library(geosphere)
set.seed(1)
DF <- tibble(
Long1 = sample(1:10),
Lat1 = sample(1:10),
Long2 = sample(1:10),
Lat2 = sample(1:10))
DF %>% mutate(
Dist = distm(x= c(Long1, Lat1), y=c(Long2, Lat2), fun = distHaversine …Run Code Online (Sandbox Code Playgroud)