小编Luk*_*Sci的帖子

提高 R 中多个嵌套 for 循环的效率

我对 R 比较陌生。我创建了代码来检查数据框并根据特定条件识别数据行,并用 1 和“检查”列标记这些行。该代码与测试数据完全按照我的预期工作。我的问题是真实的数据集有 100 万多行,虽然它可以工作,但速度太慢了。我希望能帮助提高这段代码的效率。

#create test data
alarm <- c(0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0)
setpoint <- c(10,10,10,10,10,10,10,10,8,8,9,8,8,10,10,10,10,10,10,10,10,10,10,10,8,10,10,8,10,10,10)

temp <- data.frame(alarm, setpoint)

#create a new column to capture if there is any changes to setpoint after any alarm 
temp$check <- ""

#review everyrow in dataframe
for(i in 1:nrow(temp)){
  cat(round(i/nrow(temp)*100,2),"%    \r") # prints the percentage complete in realtime.
  if(temp$alarm[i]==1 && temp$setpoint[i] >= 10){
    #for when alarm has occurred and the setpoint is 10 or above review the next 5 rows
    for(j in 0:5){ …
Run Code Online (Sandbox Code Playgroud)

performance for-loop r

11
推荐指数
4
解决办法
699
查看次数

调整 R 中帕累托图上的第二个 y 轴

我在 R 中创建了两个帕累托图表,都使用相同的数据。一个使用 ggplots stat_pareto,另一个使用 qcc 库中的 pareto.chart 函数。

ggplot(DT4, aes(x = reorder(sap_object_type_desc, -sum_duration), y = 
  sum_duration)) + 
  geom_bar(stat="identity") +
  theme(axis.text.x=element_text(angle=90,hjust=1)) + 
  stat_pareto(point.color = "red",   
              point.size = 2,        
              line.color = "black",  
              #size.line = 1,        
              bars.fill = c("blue", "orange"))
Run Code Online (Sandbox Code Playgroud)

ggplot 情节 stat_pareto

或者使用pareto.chart函数

pareto.chart(avector, 
             ylab = "Sum", 
             # xlab = "Objective Type Description", 
             main = "Avector Pareto Chart",
             cumperc = c(20,40,60,80,100)) # or = seq(0, 100, by =25)
Run Code Online (Sandbox Code Playgroud)

pareto_chart 函数

我想要做的是调整上面两个图上的第二个 y 轴,使 100% 累积百分比与最高条对齐,就像第三个例子一样。有什么建议?

示例图

r ggplot2 pareto-chart

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

标签 统计

r ×2

for-loop ×1

ggplot2 ×1

pareto-chart ×1

performance ×1