R - 忽略空?

时间:2017-10-13 21:16:11

标签: r

要求clairify:我正在寻找的东西就像if语句,如果数据框没有行,它将以某种方式忽略整个add_trace函数。

我有以下R代码:

g <- plot_ly(df, x = ~Day) %>%
  add_trace(data=df[which(df$FcNum==1),], y = ~ProjAmount, name = 'Line 1', hoverinfo = "text", text = ~paste('Projected Amt: ', round(ProjAmount,2)), mode = 'lines', width = 1, connectgaps = "false") %>% 
  add_trace(data=df[which(df$FcNum==2),], y = ~ProjAmount, name = 'Line 2', hoverinfo = "text", text = ~paste('Projected Amt: ', round(ProjAmount,2)), mode = 'lines', width = 1, connectgaps = "false") 

我的数据框如下所示:

 Day       Amount    ProjAmt      FcNum     
 1          10.4       55          1         
 1          10.4       37          2        
 2          22.1       55          1         
 2          22.1       37          2        
 3          43.5       55          1        
 3          43.5       37          2        
 4          24.1       55          1        
 4          24.1       55          2        
...         ...        ...
34          37.8       75          1        
34          37.8       99          2        
35          12.8       75          1        
35          12.8       99          2        

当我在FcNum上过滤时,我正在使用的可视化工具失败了脚本(所以当我向视觉添加过滤器并说“只给我FcNum为1的值”时)。可视化失败,因为不再有FcNum == 2的值,并且它返回以下错误消息:“错误:变量必须是长度1或1”。

(所以幕后发生的事情就是过滤数据帧看起来像这样,注意FcNum列中的值:

 Day       Amount    ProjAmt      FcNum    
 1          10.4       55          1        
 2          22.1       55          1        
 3          43.5       55          1        
 4          24.1       55          1        
...         ...        ...
34          37.8       75          1        
35          12.8       75          1  

换句话说,当代码中的第3行没有返回值时,它无法显示图形,因为它要求每个变量的值都为1或1.

是否有一个函数,可能在基数R中允许我在数据框为空时设置替代值?

我尝试过像

这样的东西
add_trace(data=na.omit(df[which(df$FcNum==2),]), y = ~ProjAmount, name = 'Line 2', hoverinfo = "text", text = ~paste('Projected Amt: ', round(ProjAmount,2)), mode = 'lines', width = 1, connectgaps = "false") 

....但这不起作用。

1 个答案:

答案 0 :(得分:0)

以下代码似乎工作正常(没有错误或警告)。我添加了type = "scatter"并删除了width = 1

# First 8 rows of your data
df <- structure(list(Day = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), Amount = c(10.4, 
10.4, 22.1, 22.1, 43.5, 43.5, 24.1, 24.1), ProjAmount = c(55L, 
37L, 55L, 37L, 55L, 37L, 55L, 55L), FcNum = c(1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L)), .Names = c("Day", "Amount", "ProjAmount", "FcNum"
), class = "data.frame", row.names = c(NA, -8L))

library(plotly)

plot_ly(df, x = ~ Day) %>%
  add_trace(
    data = df[which(df$FcNum == 1), ],
    type = "scatter",  # new line
    y = ~ ProjAmount,
    name = 'Line 1',
    hoverinfo = "text",
    text = ~ paste('Projected Amt: ', round(ProjAmount, 2)),
    mode = 'lines',
    # width = 1,
    connectgaps = "false"
  ) %>%
  add_trace(
    data = df[which(df$FcNum == 2), ],
    type = "scatter",  # new line
    y = ~ ProjAmount,
    name = 'Line 2',
    hoverinfo = "text",
    text = ~ paste('Projected Amt: ', round(ProjAmount, 2)),
    mode = 'lines',
    # width = 1,
    connectgaps = "false"
  )