如果我有一个命名向量并希望将其转换为数据帧,我能想到的所有函数都按行构造它(即它将名称-值对堆叠在一起)。
library(tibble)
x <- c(estimate = 0.595, ci.low = 0.110, ci.up = 2.004)
x
#> estimate ci.low ci.up
#> 0.595 0.110 2.004
data.frame(x)
#> x
#> estimate 0.595
#> ci.low 0.110
#> ci.up 2.004
as_tibble(x)
#> # A tibble: 3 x 1
#> value
#> <dbl>
#> 1 0.595
#> 2 0.11
#> 3 2.00
enframe(x)
#> # A tibble: 3 x 2
#> name value
#> <chr> <dbl>
#> 1 estimate 0.595
#> 2 ci.low 0.11
#> …Run Code Online (Sandbox Code Playgroud) ggplot2我已经使用创建了一个数字列表列purrr,现在我想使用将cowplot::plot_grid()它们组合成一个图。我怎样才能做到这一点?有一种蛮力方法可以做到这一点,但是当我事先不知道列表列中有多少元素时,这可能不起作用。
### libraries needed
library(tidyverse)
# install.packages("devtools")
devtools::install_github("IndrajeetPatil/ggstatsplot")
### creating list column with plots
plots <- datasets::mtcars %>%
dplyr::mutate(.data = ., cyl2 = cyl) %>%
dplyr::group_by(.data = ., cyl) %>%
tidyr::nest(data = .) %>%
dplyr::mutate(
.data = .,
plot = data %>%
purrr::map(
.x = .,
.f = ~ ggstatsplot::ggbetweenstats(
data = .,
x = am,
y = mpg,
title = as.character(.$cyl2)
)
)
)
#> Warning: aesthetic `x` was not a factor; …Run Code Online (Sandbox Code Playgroud) 我想创建一个直方图,其中有一条垂直线表示平均值,附加到该线的标签给出了平均值的确切值.
我可以轻松地创建一个垂直线的基本直方图.
# needed library
library(ggplot2)
# mean to be used later
x_mean <- mean(x = iris$Sepal.Length, na.rm = TRUE)
# creating basic plot with line for mean
(
plot <- ggplot(data = iris,
mapping = aes(x = Sepal.Length)) +
stat_bin(
col = "black",
alpha = 0.7,
na.rm = TRUE,
mapping = aes(y = ..count..,
fill = ..count..)
) +
geom_vline(
xintercept = x_mean,
linetype = "dashed",
color = "red",
na.rm = TRUE
) +
scale_fill_gradient(name = "count",
low = …Run Code Online (Sandbox Code Playgroud) I am trying to write a custom function where I want to use the cor.test function but I am having trouble unquoting the needed arguments to create a working formula.
Here is what I currently have that doesn't work-
library(rlang)
# custom function
tryfn <- function(data, x, y) {
stats::cor.test(
formula = rlang::new_formula(NULL, {{ x }} + {{ y }}),
data = data,
method = "pearson"
)
}
# using the function
tryfn(mtcars, wt, mpg)
#> Error in rlang::new_formula(NULL, {: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用dplyr和重命名我的数据框中的所有列stringr,但似乎没有按照我想要的方式工作.我应该更改以下代码以获得我想要的输出(如下面的代码所示)?
这是完全可重现的代码:
library(dplyr)
library(stringr)
library(tibble)
library(rlang)
# dataframe
x <-
tibble::as.tibble(cbind(
Grace_neu_wrong = c(1:4),
Grace_acc_wrong = c(1:4),
Grace_att_wrong = c(1:4),
Grace_int_wrong = c(1:4)
))
# defining custom function to rename the entire dataframe in a certain way
string_conversion <- function(df, ...) {
# preparing the dataframe
df <- dplyr::select(.data = df,
!!rlang::quo(...))
# custom function to split the name of each column in a certain way
splitfn <- function(x) {
x <- as.character(x)
split <- stringr::str_split(string …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个自定义函数,该函数使用chisq.test(下面是它的玩具版本)执行拟合测试的优劣。我希望函数健壮,因此我要tryCatch确保如果指定了无效的概率向量,则函数将返回带有NaNs 的数据帧。
这是功能-
set.seed(123)
# custom function
custom_prop <- function(var, ratio) {
tryCatch(
expr = broom::tidy(stats::chisq.test(
x = table(var),
p = ratio
)),
error = function(x) {
tibble::tribble(
~statistic, ~p.value, ~parameter,
NaN, NaN, NaN
)
}
)
}
Run Code Online (Sandbox Code Playgroud)
尝试有效的比率(向量总和为1;按预期工作)
custom_prop(mtcars$am, c(0.6, 0.4))
#> # A tibble: 1 x 4
#> statistic p.value parameter method
#> <dbl> <dbl> <dbl> <chr>
#> 1 0.00521 0.942 1 Chi-squared test for given probabilities
custom_prop(mtcars$am, c(0.7, 0.3))
#> …Run Code Online (Sandbox Code Playgroud) 我正在使用 ggpubr 包中的 ggscatter 绘制两个连续变量之间的相关图。我使用的是肯德尔等级系数,p 值自动添加到图表中。我想使用scale_y_log10(),因为其中一个测量值存在很大的差异。然而,在代码中添加scale_y_log10()会影响p值。
样本数据:
sampledata <- structure(list(ID = c(1, 2, 3, 4, 5), Measure1 = c(10, 10, 50, 0, 100), Measure2 = c(5, 3, 40, 30, 20), timepoint = c(1, 1,1, 1, 1), time = structure(c(18628, 19205, 19236, 19205, 19205), class = "Date"), event = c(1, 1, NA, NA, NA), eventdate = structure(c(18779,19024, NA, NA, NA), class = "Date")), row.names = c(NA, -5L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
没有scale_y_log10()的图
ggscatter(data = sampledata, x = "Measure2", y = "Measure1",
add …Run Code Online (Sandbox Code Playgroud)