我已经看到了非常类似的问题,但仍然无法解决这个简单的问题.
我想在分组变量上运行配对t检验,并将结果存储在数据帧中.tidy包broom中的命令执行此操作但是当我运行下面的代码时,输出对于每个时间点都是相同的.我知道我可以使用循环来完成它,但我想在tidy这里使用并了解发生了什么.
这是一个可重复的例子:
library(dplyr)
library(broom)
df <- data.frame (time1=rep(1:4,30),
sbp=runif(120, 100, 150),
sbp1=runif(120, 120, 170))
R>head(df)
time1 sbp sbp1
1 1 146.9411 151.4842
2 2 102.7216 139.3380
3 3 125.7126 167.1806
4 4 126.0086 146.2177
5 1 149.9213 139.7968
6 2 117.6843 135.2726
z<-df %>%
arrange(time1) %>%
group_by(time1) %>%
do(tidy(t.test(df$sbp,df$sbp1,paired=TRUE)))
Run Code Online (Sandbox Code Playgroud)
每个时间点的结果输出相同:
R>head(z)
Source: local data frame [4 x 7]
Groups: time1 [4]
time1 estimate statistic p.value parameter conf.low conf.high
(int) (dbl) (dbl) (dbl) (dbl) …Run Code Online (Sandbox Code Playgroud) 基本问题,如何将类型character LINESTRING变量转换为geometryin 类型sf,然后分别将两个坐标分成不同的变量。例如
library(tidyverse)
library(sf)
df <- structure(list(geometry = c("LINESTRING (-85.76 38.34, -85.72 38.38)",
"LINESTRING (-85.46 38.76, -85.42 38.76)",
"LINESTRING (-85.89 38.31, -85.85 38.32)"
), var1 = c(4, 5, 6
), var2 = c(1, 2, 3
)), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
))
df
# # A tibble: 3 x 3
# geometry var1 var2
# <chr> <dbl> <dbl>
# 1 LINESTRING (-85.76 38.34, -85.72 38.38) 4 1
# 2 …Run Code Online (Sandbox Code Playgroud) 我试图在管道操作结束时获得两个变量之间的相关性,为什么这些不起作用?
library(tidyverse)
iris %>%
map(~cor(.$Sepal.Length, .$Sepal.Width, use = "complete.obs"))
#or
iris %>%
dplyr::select(Sepal.Length, Sepal.Width) %>%
map2(~cor(.x, .y, use = "complete.obs"))
Run Code Online (Sandbox Code Playgroud)
谢谢
我有,我认为这是一个非常简单的问题,但无法弄明白或在网上找到确切的问题.我想按id和时间1:4对我的数据集进行排序,以便它在序列1,2,3,4而不是1,1,1,2,2,2,3,4中.见例子:
dff <- data.frame (id=c(1,1,1,1,1,1,1,1,2,2,2,3),
time=c(1,1,2,2,3,3,4,4,1,1,2,1))
R>dff
id time
1 1 1
2 1 1
3 1 2
4 1 2
5 1 3
6 1 3
7 1 4
8 1 4
9 2 1
10 2 1
11 2 2
12 3 1
Run Code Online (Sandbox Code Playgroud)
我希望生成的数据集按如下顺序排序:
R>dff
id time
1 1 1
2 1 2
3 1 3
4 1 4
5 1 1
6 1 2
7 1 3
8 1 4
9 2 1
10 2 2
11 …Run Code Online (Sandbox Code Playgroud) 我知道数据框问题列表很多,但是我找不到解决这个简单问题的方法。可以说我有:
library(tidyverse)
library(janitor)
df <- data.frame( group = c(rep("A",3), rep("B", 6)),
test_value = c(0,1,2, 0,1,2,3,4,5))
df_list <- df %>%
split(.$group) %>%
map(~tabyl(.x$test_value))
df_list
# $A
# .x$test_value n percent
# 0 1 0.3333333
# 1 1 0.3333333
# 2 1 0.3333333
# $B
# .x$test_value n percent
# 0 1 0.1666667
# 1 1 0.1666667
# 2 1 0.1666667
# 3 1 0.1666667
# 4 1 0.1666667
# 5 1 0.1666667
Run Code Online (Sandbox Code Playgroud)
我要做的就是将其转换为以下命名数据框:
A_test_value A_n A_percent B_test_value B_n B_percent
<dbl> <dbl> …Run Code Online (Sandbox Code Playgroud) 我正在尝试动态地将新变量添加到正在运行的闪亮应用程序中,但如果我开始编辑一个变量,则每次我添加其他变量时,值(文本和数字)都会重置。这个例子不需要使用 for 循环就可以工作reactiveValuesToList(),但是当我将它应用到我的代码时,它不起作用。这是我的工作示例:
library(shiny)
dist <- c("Normal", "Gamma")
ui <- shinyUI(fluidPage(
sidebarPanel(
actionButton("add_btn", "Add Textbox"),
actionButton("rm_btn", "Remove Textbox"),
textOutput("counter")
),
mainPanel(uiOutput("textbox_ui"))
))
server <- shinyServer(function(input, output, session) {
# Track the number of input boxes to render
counter <- reactiveValues(n = 0)
# Track all user inputs
AllInputs <- reactive({
x <- reactiveValuesToList(input)
})
observeEvent(input$add_btn, {counter$n <- counter$n + 1})
observeEvent(input$rm_btn, {
if (counter$n > 0) counter$n <- counter$n - 1
})
output$counter <- renderPrint(print(counter$n))
textboxes …Run Code Online (Sandbox Code Playgroud) 这似乎是一个基本问题,但我找不到对此的解释。
我可以这样命名一个向量:
values = c("lab1" = "my_lab1",
"lab2" = "my_lab2")
values
# lab1 lab2
# "my_lab1" "my_lab2"
Run Code Online (Sandbox Code Playgroud)
我可以通过使用paste0创建元素来创建相同的向量,如下所示:
values = c("lab1" = "my_lab1",
"lab2" = paste0("my_lab", "2"))
values
# lab1 lab2
# "my_lab1" "my_lab2"
Run Code Online (Sandbox Code Playgroud)
但是当您尝试使用 创建名称时paste0,会导致错误:
values = c("lab1" = "my_lab1",
paste0("lab", "2") = "my_lab2")
# Error: unexpected '=' in:
# "values = c("lab1" = "my_lab1",
# paste0("lab", "2") ="
Run Code Online (Sandbox Code Playgroud)
为什么是这样?考虑到以下情况属实:
identical("lab2", paste0("lab", "2"))
[1] TRUE
Run Code Online (Sandbox Code Playgroud)
解决方案是使用setNames,但我对为什么上述问题很感兴趣:
setNames(c("my_lab1", "my_lab2"), c("lab1", paste0("lab", "2")))
# …Run Code Online (Sandbox Code Playgroud) 我希望将图的图例从垂直线旋转为水平线,但保持图表中的实际线垂直。有许多黑客类型的解决方案,但我认为现在可以通过包轻松完成此操作ggstance,但我不确定如何实现它。
library(tidyverse)
library(ggstance)
df <- tibble(x = rnorm(40))
df_stats <-
df %>% summarise(
mean = mean(x),
median = median(x)
) %>%
gather(key = legend, value = value, mean:median)
df %>%
ggplot(aes(x = x)) +
geom_histogram(bins = 20) +
geom_vline(data = df_stats, aes(xintercept = value, color = legend))
Run Code Online (Sandbox Code Playgroud)
有什么建议使用ggstance吗?谢谢
根据@Allan Cameron 评论进行编辑
由于ggstance已被最新版本的 ggplot 取代,我很高兴看到有人可能有任何解决方案?
dplyr不喜欢我的大数据集,所以我尝试将以下简单代码转换为最有效的data.table等效代码:
library(tidyverse)
data(iris)
iris$year <- rep(c(2000, 3000), each = 25)
iris$color <- rep(c("red", "green","blue"), each = 50)
iris$letter <- as.factor(rep(c("A", "B", "C"), each = 50))
head(iris, 3)
iris %>%
group_by(Species, year) %>%
summarise(across(c(-Sepal.Length, -Sepal.Width), dplyr::first),
across(c(Sepal.Length, Sepal.Width), dplyr::last)) %>%
ungroup
Run Code Online (Sandbox Code Playgroud)
然而,我的努力给了我错误的解决方案,也没有命名列:
library(data.table)
final <- setDT(iris)[, c(
lapply(setdiff(names(iris), c("Sepal.Length", "Sepal.Width")), head, 1),
lapply(c("Sepal.Length", "Sepal.Width"), tail, 1)
), by = c("Species", "year")]
final
Run Code Online (Sandbox Code Playgroud)
也许有更快/更好的data.table方法?
谢谢
编辑
当我让上面的dplyr代码在我的真实数据(约 300 万行,80 列)上运行时,我遇到了内存问题。它rstudio在中止前运行了大约 15 小时。summarise …
类似于这个问题,给出tmpp:
library(data.table)
library(tidyverse)
tmpp <- data.table(
"ID" = c(1,1,1,2,2),
"Date" = c(1,2,3,1,2),
"total_neg" = c(1,1,0,0,2),
"total_pos" = c(4,5,2,4,5),
"H1" = c(5,4,0,5,-5),
"H2" = c(5,-10,5,5,-5),
"H3" = c(-10,6,5,0,10)
)
tmpp
# ID Date total_neg total_pos H1 H2 H3
# 1: 1 1 1 4 5 5 -10
# 2: 1 2 1 5 4 -10 6
# 3: 1 3 0 2 0 5 5
# 4: 2 1 0 4 5 5 0
# 5: 2 …Run Code Online (Sandbox Code Playgroud)