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

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

How to change a tidyverse syntax into data.table

问题

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

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

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

英文:

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

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

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

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

But I encountered an error message with this:

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

What's wrong with this code?

答案1

得分: 1

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

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

总的来说,

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

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

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

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

希望这有所帮助!

英文:

I think this should work:

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

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):

  1. mtcars[, .(n = .N), by = .(cyl)]
  2. # Error in `[.data.frame`(mtcars, , .(n = .N), by = .(cyl)) :
  3. # unused argument (by = .(cyl))
  4. as.data.table(mtcars)[, .(n = .N), by = .(cyl)]
  5. # cyl n
  6. # &lt;num&gt; &lt;int&gt;
  7. # 1: 6 7
  8. # 2: 4 11
  9. # 3: 8 14
  10. MT &lt;- mtcars
  11. setDT(MT)
  12. MT[, .(n = .N), by = .(cyl)]
  13. # cyl n
  14. # &lt;num&gt; &lt;int&gt;
  15. # 1: 6 7
  16. # 2: 4 11
  17. # 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:

确定