如何将tidyverse语法转换为data.table

huangapple go评论100阅读模式
英文:

How to change a tidyverse syntax into data.table

问题

以下是已翻译的代码部分:

dfMktHop1_Z1 <- reactive({
  data <- ZoneHop1.1()
  
  data_count <- data[, .N, by = .(codeGeo, Hopital)]
  
  data_tally <- data_count[, .(sejour.com = sum(N)), by = codeGeo]
  
  data_filtered <- data_tally[Hopital == input$Hopital1, .(codeGeo, Hopital1 = Hopital, Sej.Hop1 = sejour.com)]
  
  setnames(data_filtered, "codeGeo", "Code")
  
  return(data_filtered)
})

这个错误信息是因为你在使用data.frame[.data.frame子集操作时,使用了不支持的参数。请确保你的R包版本正常,并确保你的数据表是正确的data.table对象。如果你的包版本和数据都没问题,

英文:

I want to change a part of my shiny code in data.table.

dfMktHop1_Z1&lt;-reactive({ZoneHop1.1()%&gt;%
      group_by(codeGeo,Hopital)%&gt;%count()%&gt;%# sejour par commune et par hopital
      ungroup()%&gt;%group_by(codeGeo)%&gt;%add_tally(n,name=&quot;sejour.com&quot;)%&gt;%# sejour tot commune
      filter(Hopital==input$Hopital1)%&gt;%select(codeGeo,Hopital,n,sejour.com)%&gt;%
      rename(Hopital1=Hopital,Sej.Hop1=n)})

I asked ChatGPT to transform it into data.table :
This is what it suggested;

dfMktHop1_Z1 &lt;- reactive({
  data &lt;- ZoneHop1.1()
  
  data_count &lt;- data[, .N, by = .(codeGeo, Hopital)]
  
  data_tally &lt;- data_count[, .(sejour.com = sum(N)), by = codeGeo]
  
  data_filtered &lt;- data_tally[Hopital == input$Hopital1, .(codeGeo, Hopital1 = Hopital, Sej.Hop1 = sejour.com)]
  
  setnames(data_filtered, &quot;codeGeo&quot;, &quot;Code&quot;)
  
  return(data_filtered)
})

But I encountered an error message with this:

Error in [.data.frame: unused argument (by = .(codeGeo, Hopital))

What's wrong with this code?

答案1

得分: 1

以下是已翻译的代码部分:

dfMktHop1_Z1 <- reactive({
  req(ZoneHop1.1())
  as.data.table(ZoneHop1.1()) %>%
    .[, .(n = .N), by = .(codeGeo, Hopital) ] %>%
    .[, sejour.com := .N, by = .(codeGeo) ] %>%
    .[Hopital == input$Hopital1, .(codeGeo, Hopital1 = Hopital, Hop1 = n, sejour.com)]
})

总的来说,

  • group_by %&gt;% count 相当于 .[, .(n = .N), by = .(..)]
  • add_tally 类似,但只是加法,.[, n := .N, by = .(..)]
  • filter 是直接的翻译

我将 filterselect 结合成了一个单一的表达式,看起来更有意义。

我怀疑你出现错误的原因,正如 Stefan 指出的,可能是你试图在 data.frame 上进行操作,而不是在 data.table 上。作为演示,你可以使用 as.data.tablesetDT 中的任一方法:

mtcars[, .(n = .N), by = .(cyl)]
# Error in `[.data.frame`(mtcars, , .(n = .N), by = .(cyl)) : 
#   unused argument (by = .(cyl))
as.data.table(mtcars)[, .(n = .N), by = .(cyl)]
#      cyl     n
#    &lt;num&gt; &lt;int&gt;
# 1:     6     7
# 2:     4    11
# 3:     8    14
MT <- mtcars
setDT(MT)
MT[, .(n = .N), by = .(cyl)]
#      cyl     n
#    &lt;num&gt; &lt;int&gt;
# 1:     6     7
# 2:     4    11
# 3:     8    14

希望这有所帮助!

英文:

I think this should work:

dfMktHop1_Z1 &lt;- reactive({
  req(ZoneHop1.1())
  as.data.table(ZoneHop1.1()) %&gt;%
    .[, .(n = .N), by = .(codeGeo, Hopital) ] %&gt;%
    .[, sejour.com := .N, by = .(codeGeo) ] %&gt;%
    .[Hopital == input$Hopital1, .(codeGeo, Hopital1 = Hopital, Hop1 = n, sejour.com)]
})

In general,

  • group_by %&gt;% count is equivalent to .[, .(n = .N), by = .(..)]
  • add_tally is similar but just additive, .[, n := .N, by = .(..)]
  • filter is a direct translation

I combined the filter and select into a single expression, seemed to make sense.

I suspect that the cause of your error, as stefan highlighted, is that you are trying this on a data.frame and not a data.table. For demonstration, using either method (as.data.table or setDT):

mtcars[, .(n = .N), by = .(cyl)]
# Error in `[.data.frame`(mtcars, , .(n = .N), by = .(cyl)) : 
#   unused argument (by = .(cyl))
as.data.table(mtcars)[, .(n = .N), by = .(cyl)]
#      cyl     n
#    &lt;num&gt; &lt;int&gt;
# 1:     6     7
# 2:     4    11
# 3:     8    14
MT &lt;- mtcars
setDT(MT)
MT[, .(n = .N), by = .(cyl)]
#      cyl     n
#    &lt;num&gt; &lt;int&gt;
# 1:     6     7
# 2:     4    11
# 3:     8    14

huangapple
  • 本文由 发表于 2023年8月10日 18:35:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定