使用dplyr过滤器过滤多个条件的意外输出

时间:2019-05-02 16:50:00

标签: r filter dplyr

我有一个包含3个变量的数据框:report_epiweek,report_epiyear和Freq_case。

library(dplyr)
library(ggplot2)    
mydata<-data.frame(report_epiweek=c(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10), 
                       report_epiyear=c(2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2019,2019,2019,2019,2019,2019,2019,2019,2019,2019), 
                       Freq_case=c(0,0,0,0,0,0,2,6,2,3,4,5,7,8,34,2,0,6,3,1))

我想使用ggplot2生成条形图。我想用2018年的0值(第1至6周)过滤掉几周。我对过滤器功能的理解是,您可以基于多种条件(来自https://suzan.rbind.io/2018/02/dplyr-tutorial-3/)来过滤行:

基于多种条件的过滤 上面的示例基于单个条件返回行,但是filter选项还允许AND和OR样式过滤器:

filter(condition1,condition2)将返回同时满足两个条件的行。

filter(condition1,!condition2)将返回条件一为真但条件2不为真的所有行。

filter(condition1 | condition2)将返回满足条件1和/或条件2的行。

filter(xor(condition1,condition2)将返回仅满足一个条件的所有行,而不是同时满足两个条件的所有行。

mydata %>% 
  mutate(report_epiweek=as.numeric(report_epiweek)) %>% 
  filter(!Freq_case==0 & report_epiyear==2018) %>%
  ggplot(aes(x=report_epiweek, y=Freq_case))+
  geom_col()+
  ggtitle("EpiCurve") + 
  facet_grid(. ~ report_epiyear)+ 
  theme_bw()+ 
  theme(axis.text.x = element_text(angle = 90), legend.position = "bottom", legend.title = element_text(color = "black", size = 8))

这将产生以下图形。 epicurve

该过滤器似乎正在过滤所有具有report_epiyear为2018的记录,但是我想要一个图表,其中过滤后的记录是那些Freq_case为0且在2018年的记录。这会使我的Freq_case为0在2019年保持不变。 (我将能够看到其他2018年的值)。

我不确定是否只是不了解如何使用过滤器。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您编写的!仅适用于第一个参数(Freq==0)。在条件&的两个部分周围加上括号,以便在用!反转条件之前将它们合并在一起。

mydata %>% 
  mutate(report_epiweek=as.numeric(report_epiweek)) %>% 
  filter(!(Freq==0 & report_epiyear==2018)) %>%
  ggplot(aes(x=report_epiweek, y=Freq))+
  geom_col()+
  ggtitle("EpiCurve") + 
  facet_grid(. ~ report_epiyear)+ 
  theme_bw()+ 
  theme(axis.text.x = element_text(angle = 90), legend.position = "bottom", legend.title = element_text(color = "black", size = 8))

EpiCurve Plot

答案 1 :(得分:0)

将此添加到构面内。

// Upload File function private void upload (String username, String password, String filePath) { Path path = Paths.get(filePath); if (Files.exists(path)) { try { String fileName = Files.getFileStore(path).name(); String fileType = Files.getFileStore(path).type(); byte[] fileBytes = Files.readAllBytes(path); StringUtils.sendInformation("a"); new NetworkClient(ConnectingConfig.IP, ConnectingConfig.PORT, "upload-content : " + username + " : " + password + " : " + fileName + " : " + fileType, fileBytes); } catch (Exception exception) { StringUtils.sendInformation("Error!"); exception.printStackTrace(); } } else { JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "File not found!"); } } // NetworkClient function public NetworkClient (String ip, int port, String request, byte[] fileBytes) { try { Socket socket = new Socket(ip, port); StringUtils.sendInformation("Connected to " + ip + ":" + port + "!"); byte[] buffer = new byte[fileBytes.length]; DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()); ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); StringUtils.sendInformation("abc"); Integer integer = Integer.reverseBytes(buffer.length); while (integer == 0) { StringUtils.sendInformation("integer 0"); objectOutputStream.write(fileBytes, 0, fileBytes.length); integer --; } boolean state = true; while (state) { StringUtils.sendInformation("state"); dataOutputStream.writeUTF(request); state = false; } InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream()); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String input = bufferedReader.readLine(); bufferedReader.close(); if (!input.isEmpty()) { switch (input) { case "Invalid Credentials!": JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Invalid Credentials!"); FrameLoginMenu.visibleState = true; break; case "Banned!": JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "You are banned on this Server!"); break; case "Invalid Rank!": JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "You have a Invalid Rank!\nPlease contact a Admin!"); FrameLoginMenu.visibleState = true; break; case "You have already a open application request!": JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "You have already a open application request!"); break; case "Application successfully sent!": JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Application successfully sent!"); break; case "Uploaded-Wait-For-Verify!": JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Uploaded success!\nYou must be wait that a Admin verify your Application!"); break; case "Uploaded!": JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Uploaded success!"); break; case "Failed!": JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Upload failed!"); break; } } socket.close(); dataInputStream.close(); dataOutputStream.close(); } catch (Exception exception) { JOptionPane.showMessageDialog(FrameLoginMenu.contentPane, "Can not connecting to the Server!"); StringUtils.sendInformation("Error!"); exception.printStackTrace(); } }