小编Eri*_*een的帖子

通过列名的复杂模式的子集数据帧

我有一个如下所示的数据集:

  • 两轮数据(.t0.t1)
  • 多尺度(thisthat)
  • 每一等级的几个项目(1,22,22a)
  • 几个变量忽略(v2,v3,ignore.t0,ignore.t1,this.t0,this.t1,that.t0,that.t1)

.

dat <- data.frame(id = seq(from=1, to=10, by=1),
                  v2 = rnorm(10),
                  v3 = rnorm(10),
                  ignore.t0 = rnorm(10),
                  this.t0 = rnorm(10),
                  this1.t0 = rnorm(10),
                  this22.t0 = rnorm(10),
                  this22a.t0 = rnorm(10),
                  that.t0 = rnorm(10),
                  that1.t0 = rnorm(10),
                  that22.t0 = rnorm(10),
                  that22a.t0 = rnorm(10),
                  ignore.t1 = rnorm(10),
                  this.t1 …
Run Code Online (Sandbox Code Playgroud)

regex grep r

3
推荐指数
1
解决办法
1345
查看次数

线标签位于绘图右侧且不重叠

我正在尝试从我们的数据世界中重现此图表

\n

在此输入图像描述

\n

我正在寻找使行标签看起来尽可能接近原始标签的方法。这是我到目前为止所得到的(显示版本ggrepel(),请参阅注释掉的行以获取替代方案):

\n
library(tidyverse)\nlibrary(ggrepel)\nkeep <- c("Israel", "United Arab Emirates", "United Kingdom",\n          "United States", "Chile", "European Union", "China",\n          "Russia", "Brazil", "World", "Mexico", "Indonesia",\n          "Bangladesh")\n\nowid <- read_csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations.csv") %>%\n  filter(location %in% keep) %>%\n  filter(date >= "2021-01-01" & date <= "2021-02-12") %>%\n  select(location, date, total_vaccinations_per_hundred) %>%\n  arrange(location, date) %>%\n  group_by(location) %>%\n  complete(date = seq.Date(as.Date("2021-01-01"), \n                           as.Date("2021-02-12"), \n                           by="day")) %>%\n  fill(total_vaccinations_per_hundred) %>%\n  ungroup() %>%\n  mutate(location = factor(location),\n         location = fct_reorder2(location, total_vaccinations_per_hundred,\n                                 total_vaccinations_per_hundred)) %>%\n  mutate(label = if_else(date == max(date), …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

2
推荐指数
1
解决办法
2239
查看次数

找不到闪亮的反应对象

我认为我的Shiny应用程序中缺少与反应性有关的某些内容。这是MRE,显示我的打印问题y

library(shiny)

ui <- fluidPage(
   titlePanel("Test"),
   textAreaInput("text", "Text", "", width = "400px"),
   verbatimTextOutput("text"),
   actionButton("do", "Run"),
   textOutput("result")
   )

server <- function(input, output) {
  observeEvent(input$do, {
  y <- reactive({
    x <- ""
    t <- 1
    while (t < 5) {
      x <- paste(input$text, x, sep=",")
      t <- t + 1
    }
  })
  })
  output$result <- renderPrint({y})
}

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

r shiny

1
推荐指数
1
解决办法
981
查看次数

unnest_auto 和 unnest_longer 用于取消多列的嵌套

我有一个嵌套的数据框,我正在尝试取消嵌套。这是该结构的一个假示例。

df <- structure(list(`_id` = c("a", "b", "c", "d"), 
                     variable = list(structure(list(type = c("u", "a", "u", "a", "u", "a", "a"), 
                                                    m_ = c("m1",
                                                           "m2",
                                                           "m3",
                                                           "m4",
                                                           "m5",
                                                           "m6", "m7"), #omitted from original example by mistake 
                                                    t_ = c("2015-07-21 4:13 PM", 
                                                           "2016-04-21 7:25 PM", 
                                                           "2017-10-04 9:49 PM", 
                                                           "2018-12-04 12:29 PM", 
                                                           "2019-04-20 20:20 AM", 
                                                           "2016-05-20 12:00 AM", 
                                                           "2016-06-20 12:00 AM"), 
                                                    a_ = c(NA, 
                                                           "", 
                                                           NA, 
                                                           "", 
                                                           NA, 
                                                           "C", 
                                                           "C")), 
                                               class = "data.frame", 
                                               row.names = c(NA, 7L)), 
                                     structure(list(type = c("u", "a"), 
                                                    m_ = …
Run Code Online (Sandbox Code Playgroud)

r tidyr

1
推荐指数
1
解决办法
2339
查看次数

解析一列json并与另一列绑定以制作数据框

我有采用以下格式的数据:

have <- structure(list(V1 = c(4L, 28L, 2L), 
                       V2 = c("[{\"group\":1,\"topic\":\"A\"},{\"group\":1,\"topic\":\"B\"},{\"group\":2,\"topic\":\"C\"},{\"group\":2,\"topic\":\"T\"},{\"group\":2,\"topic\":\"U\"},{\"group\":3,\"topic\":\"V\"},{\"group\":3,\"topic\":\"D\"},{\"group\":3,\"topic\":\"R\"},{\"group\":4,\"topic\":\"A\"},{\"group\":4,\"topic\":\"Q\"},{\"group\":4,\"topic\":\"S\"},{\"group\":4,\"topic\":\"W\"},{\"group\":6,\"topic\":\"O\"},{\"group\":6,\"topic\":\"P\"},{\"group\":6,\"topic\":\"E\"},{\"group\":6,\"topic\":\"F\"},{\"group\":6,\"topic\":\"G\"},{\"group\":6,\"topic\":\"H\"},{\"group\":6,\"topic\":\"I\"},{\"group\":6,\"topic\":\"J\"},{\"group\":6,\"topic\":\"K\"},{\"group\":6,\"topic\":\"L\"},{\"group\":6,\"topic\":\"M\"},{\"group\":6,\"topic\":\"N\"}]",
                              "[]", 
                              "[{\"group\":2,\"topic\":\"C\"},{\"group\":3,\"topic\":\"D\"},{\"group\":6,\"topic\":\"O\"},{\"group\":6,\"topic\":\"P\"},{\"group\":6,\"topic\":\"E\"},{\"group\":6,\"topic\":\"G\"},{\"group\":6,\"topic\":\"M\"}]")
), 
row.names = c(NA, 3L), 
class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

的内容V2是每一行的嵌套分组,如[{"group":1,"topic":"A"},{"group":1,"topic":"B"}...]

我想获得一个宽数据框,其中also_have每行的 group+topic 的每个组合都有一个指示符 (1/0)(请参阅 参考资料)。像这样的东西:

# A tibble: 3 x 4
     id topic_id_1 topic_id_2 topic_id_3 topic_id_4 ...
  <dbl>      <dbl>      <dbl>      <dbl>
1     4          1          1          0
2    28          0          0          0
3     2          0          0          0
Run Code Online (Sandbox Code Playgroud)

第一步是解析json。

我可以使用purrr::map(have$V2, jsonlite::fromJSON)取消嵌套到列表中,但我不确定如何将V1列(我们可能会重命名为id)绑定到结果列表的每个元素(请注意,列表元素二是空的,因为它V1==28是空的)。下面是添加id( V1) …

r jsonlite purrr

1
推荐指数
1
解决办法
24
查看次数

标签 统计

r ×5

ggplot2 ×1

grep ×1

jsonlite ×1

purrr ×1

regex ×1

shiny ×1

tidyr ×1