首页 > 解决方案 > 提高使用向量提取一行数据的一行代码的速度

问题描述

我有一个如下的 for 循环(这是可以在此处看到的函数的一部分):

People_met <- c(15L, 66L)

for (j in 1:length(People_met)) {
                # Grab who they will meet
                Meetingsa <- Data[People_met[j], ]
                # If exposed change State
                if(Meetingsa$State== "Exposed") {
                    Urand1 <- runif(1,0,1)
                    if (Urand1 < parameters$S2E){
                        Data$State[i] <-  "Exposed"
                    }   
                }
            }        

我曾经profvis确定Meetingsa <- Data[People_met[j], ]这个 for 循环的一部分需要很长时间(正确的线测量时间)。

在此处输入图像描述

我想加快速度,例如使用data.table. 然而,我实际上最终显着减慢了速度。我不确定还能尝试什么。有人可以向我解释哪些选择会更快吗?

数据

Data <- structure(list(AgentNo = 1:100, State = c("Exposed", "Exposed", 
"Exposed", "Exposed", "Exposed", "Infected", "Infected", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible", "Susceptible", "Susceptible", "Susceptible", 
"Susceptible", "Susceptible"), Mixing = c(0.832540284609422, 
0.0231213096994907, 0.152527641970664, 0.354236921761185, 0.237191909225658, 
0.828387739369646, 0.746136361733079, 0.578939395956695, 0.758876445936039, 
0.424538396764547, 0.229595123790205, 0.672923852456734, 0.61330992076546, 
0.374492820119485, 0.583008445333689, 0.94473810470663, 0.25385448615998, 
0.674593120580539, 0.869715234031901, 0.94868006859906, 0.229183854768053, 
0.399804306449369, 0.0552410976961255, 0.153342365752906, 0.20760683552362, 
0.939011836657301, 0.0690710921771824, 0.944459688384086, 0.311271844198927, 
0.795019918819889, 0.935355108231306, 0.559848339296877, 0.160864517092705, 
0.0566015436779708, 0.986112118931487, 0.109344870317727, 0.711717555997893, 
0.168269659625366, 0.967747716465965, 0.0870282740797848, 0.311811906984076, 
0.00380628812126815, 0.677446827525273, 0.949922770261765, 0.596800298895687, 
0.0406050367746502, 0.438798732124269, 0.677494721952826, 0.869463230017573, 
0.954477905295789, 0.728465437423438, 0.0826929600443691, 0.762347809504718, 
0.144062878331169, 0.17963783419691, 0.627232523402199, 0.88223961670883, 
0.0266130077652633, 0.844961556373164, 0.259414804400876, 0.174613558454439, 
0.964706963393837, 0.111714730970562, 0.869064266094938, 0.0844376638997346, 
0.173526355763897, 0.325475359801203, 0.706891416572034, 0.289788722060621, 
0.315327962161973, 0.250811351696029, 0.256510636769235, 0.893950980389491, 
0.702868187101558, 0.1915901475586, 0.883692211238667, 0.596465814160183, 
0.992735084844753, 0.999628762481734, 0.568414112087339, 0.588263624813408, 
0.0249269008636475, 0.27322138636373, 0.464655231451616, 0.449471640633419, 
0.919530394952744, 0.809842582559213, 0.0786217574495822, 0.890167841454968, 
0.617845843313262, 0.521652236580849, 0.522370660211891, 0.591729528969154, 
0.243999561062083, 0.742002451326698, 0.693615336669609, 0.800648423610255, 
0.31477079144679, 0.0315601106267422, 0.563548003323376), TimeE = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TimeI = c(0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Time = c(8, 9, 4, 
9, 4, 6, 3, 4, 9, 4, 8, 9, 4, 9, 4, 8, 9, 4, 9, 4, 8, 9, 4, 9, 
4, 8, 9, 4, 9, 4, 8, 9, 4, 9, 4, 8, 9, 4, 9, 4, 8, 9, 4, 9, 4, 
8, 9, 4, 9, 4, 8, 9, 4, 9, 4, 8, 9, 4, 9, 4, 8, 9, 4, 9, 4, 8, 
9, 4, 9, 4, 8, 9, 4, 9, 4, 8, 9, 4, 9, 4, 8, 9, 4, 9, 4, 8, 9, 
4, 9, 4, 8, 9, 4, 9, 4, 8, 9, 4, 9, 4)), row.names = c(NA, -100L
), class = "data.frame")

标签: rfunctionperformancefor-loop

解决方案


dplyr这是一个不使用for循环的示例工作流程。

People_met <- c(15L, 66L)
paramters <- list(S2E = 0.5)

library(dplyr)
set.seed(100)
Data %>% 
  filter(AgentNo %in% People_met) %>% #filter relevant rows
  mutate(
    r_unif = runif(n()), #create random uniform sample 
    State = ifelse(State == "Exposed" & r_unif < paramters$S2E, "Exposed", "Whatelse")
  )

推荐阅读