英文:
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<-reactive({ZoneHop1.1()%>%
group_by(codeGeo,Hopital)%>%count()%>%# sejour par commune et par hopital
ungroup()%>%group_by(codeGeo)%>%add_tally(n,name="sejour.com")%>%# sejour tot commune
filter(Hopital==input$Hopital1)%>%select(codeGeo,Hopital,n,sejour.com)%>%
rename(Hopital1=Hopital,Sej.Hop1=n)})
I asked ChatGPT to transform it into data.table :
This is what it suggested;
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)
})
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 %>% count
相当于.[, .(n = .N), by = .(..)]
add_tally
类似,但只是加法,.[, n := .N, by = .(..)]
filter
是直接的翻译
我将 filter
和 select
结合成了一个单一的表达式,看起来更有意义。
我怀疑你出现错误的原因,正如 Stefan 指出的,可能是你试图在 data.frame
上进行操作,而不是在 data.table
上。作为演示,你可以使用 as.data.table
或 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
# <num> <int>
# 1: 6 7
# 2: 4 11
# 3: 8 14
MT <- mtcars
setDT(MT)
MT[, .(n = .N), by = .(cyl)]
# cyl n
# <num> <int>
# 1: 6 7
# 2: 4 11
# 3: 8 14
希望这有所帮助!
英文:
I think this should work:
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)]
})
In general,
group_by %>% 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
# <num> <int>
# 1: 6 7
# 2: 4 11
# 3: 8 14
MT <- mtcars
setDT(MT)
MT[, .(n = .N), by = .(cyl)]
# cyl n
# <num> <int>
# 1: 6 7
# 2: 4 11
# 3: 8 14
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论