识别并分组观察

时间:2019-03-06 22:04:06

标签: r dplyr

如何识别并生成一个新变量,该变量标识哪些观测值属于不同的组。说我有以下数据集:

ID | country  |  side  
1  |  arg     |  1
1  |  usa     |  0
2  |  ita     |  1
2  |  usa     |  0
2  |  uk      |  1
3  |  aus     |  0
3  |  uk      |  1

,我想创建一个新变量(sideUK),以识别国家“ uk”是否涉及每个国家的ID和身份。因此,例如:

ID | country  |  side  | sideuk
1  |  arg     |  1     |  0
1  |  usa     |  0     |  0
2  |  ita     |  1     |  1
2  |  usa     |  0     |  0
2  |  uk      |  1     |  1
3  |  aus     |  0     |  0
3  |  uk      |  1     |  1

3 个答案:

答案 0 :(得分:2)

我不确定您要做什么,但是以下内容可以再现您的预期输出

library(dplyr)
df %>%
    group_by(ID) %>%
    mutate(sideuk = +("uk" %in% country & side == 1)) %>%
    ungroup()
## A tibble: 7 x 4
#     ID country  side sideuk
#  <int> <fct>   <int>  <int>
#1     1 arg         1      0
#2     1 usa         0      0
#3     2 ita         1      1
#4     2 usa         0      0
#5     2 uk          1      1
#6     3 aus         0      0
#7     3 uk          1      1

样本数据

df <- read.table(text =
    "ID  country    side
1    arg       1
1    usa       0
2    ita       1
2    usa       0
2    uk        1
3    aus       0
3    uk        1", header = T)

答案 1 :(得分:2)

您要按ID分组,然后在'uk'变量中检查country

df %>%
    group_by(ID, side) %>%
    mutate(sideuk = as.integer('uk' %in% country))

# A tibble: 7 x 4
# Groups:   ID, side [6]
     ID country  side sideuk
  <dbl> <fct>   <dbl>  <int>
1     1 arg         1      0
2     1 usa         0      0
3     2 ita         1      1
4     2 usa         0      0
5     2 uk          1      1
6     3 aus         0      0
7     3 uk          1      1

答案 2 :(得分:0)

我不确定这是否是您想要的。这是没有外部库的解决方案:

ui = fluidPage(
  titlePanel("opening web pages"),
  sidebarPanel(selectInput(
    inputId = 'test',
    label = 1,
    choices = c("sample", "zoo", "product")
  )),
  mainPanel(htmlOutput("inc"))
)

server = function(input, output) {
  myhtmlfilepath <- getwd() # change to your path
  addResourcePath('myhtmlfiles', myhtmlfilepath)

  getPage <- function() {
    return(tags$iframe(src = paste0("myhtmlfiles/", input$test, ".html"), height = "100%", width = "100%", scrolling = "yes"))
  }

  output$inc <- renderUI({
    req(input$test)
    getPage()
  })
}

shinyApp(ui, server)

返回:

df$sideuk <- apply(df, 1, function(row) {
  return(
    as.integer(any(df[df$ID==row["ID"] & df$country=="uk" & row["side"] == 1, "side"]))
  )
})

样本数据

  ID country side sideuk
1  1     arg    1      0
2  1     usa    0      0
3  2     ita    1      1
4  2     usa    0      0
5  2      uk    1      1
6  3     aus    0      0
7  3      uk    1      1
8  4      mx    1      0
9  4      uk    0      0