我有一个数据框,每行一个观察值,每个主题两个观察值。我只想过滤出重复的“天”号行。
ex <- data.frame('id'= rep(1:5,2), 'day'= c(1:5, 1:3,5:6))
Run Code Online (Sandbox Code Playgroud)
以下代码仅过滤掉第二个重复的行,而不过滤掉第一行。同样,我想过滤掉两个重复的行。
ex %>%
group_by(id) %>%
filter(duplicated(day))
Run Code Online (Sandbox Code Playgroud)
以下代码有效,但看起来很笨拙。有谁有更有效的解决方案?
ex %>%
group_by(id) %>%
filter(duplicated(day, fromLast = TRUE) | duplicated(day, fromLast = FALSE))
Run Code Online (Sandbox Code Playgroud) 我有以下数据集,其中三列包含日期。
library(dplyr)
set.seed(45)
df1 <- data.frame(hire_date = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="week"), 10),
t1 = sample(seq(as.Date('2000/01/01'), as.Date('2001/01/01'), by="week"), 10),
t2 = sample(seq(as.Date('2000/01/01'), as.Date('2001/01/01'), by="day"), 10))
#this value is actually unknown
df1[10,2] <- NA
hire_date t1 t2
1 1999-08-20 2000-05-13 2000-02-17
2 1999-04-23 2000-11-11 2000-04-27
3 1999-03-26 2000-04-15 2000-08-01
4 1999-05-07 2000-06-03 2000-08-29
5 1999-04-30 2000-05-27 2000-11-19
6 1999-04-09 2000-12-30 2000-01-26
7 1999-03-12 2000-12-23 2000-12-07
8 1999-06-25 2000-02-12 2000-09-26
9 1999-02-26 2000-05-06 2000-08-23
10 1999-01-01 <NA> 2000-03-18
Run Code Online (Sandbox Code Playgroud)
我想执行 if else 语句,如果 t1 OR …
我试图在 tabBox 中的两个单独的选项卡面板中以闪亮的方式绘制相同的直方图。我可以在其中一个选项卡中绘制数据,但是当我添加另一个选项卡的代码时,它似乎破坏了应用程序。下面是我正在尝试做的一个例子:
library(shiny)
library(dplyr)
data(mtcars)
body <- dashboardBody(
fluidRow(
tabBox(
title = "miles per gallon",
id = "tabset1", height = "250px",
tabPanel("Tab1", plotOutput("plot1")),
tabPanel("Tab2", plotOutput("plot1"), "test") # the app 'breaks' when I add in the **plotOutput("plot1")** here... however it works when I remove it
)
)
)
shinyApp(
ui = dashboardPage(
dashboardHeader(title = "Test"),
dashboardSidebar(),
body
),
server = function(input, output) {
output$plot1 <- renderPlot({hist(mtcars$mpg)})
}
)
Run Code Online (Sandbox Code Playgroud)
在这个特定的示例中,我可以像这样在服务器中添加另一行
output$plot2 <- renderPlot({hist(mtcars$mpg)})
Run Code Online (Sandbox Code Playgroud)
然后调用plot2,但我的实际应用程序比上面的示例更复杂一些,所以我想在两个选项卡中绘制plot1。
我有以下数据框
library(ggplot2)
set.seed(149)
x <- data.frame(
region = factor(rep(1:10, each = 2)),
group = rep(c("O", "E"), 10),
mean = sample(1:2, 20, replace = TRUE)
)
x
region group mean
1 1 O 2
2 1 E 1
3 2 O 1
4 2 E 2
5 3 O 1
6 3 E 1
7 4 O 1
8 4 E 1
9 5 O 1
10 5 E 2
11 6 O 2
12 6 E 2
13 7 O …Run Code Online (Sandbox Code Playgroud)