Yuan Chun Ding
2021-Oct-26 22:38 UTC
[R] how to include if conditions in dplyr filter function
Hi R users, I thought the follow R code should work, but I got error, Can you fix my code? Thank you, Ding outlier_tcga_MAD3 <- outlier_tcga %>% filter(n_two >0) %>% mutate(freqMAD3_gain2ratio = N_MAD3_gain2/n_two )%>% if (N_MAD3 < 9) {filter(freqMAD3_gain >=1)} else if (N_MAD3 > n_two*2 ) {filter (freqMAD3_gain >= 0.8 & freqMAD3_gain2ratio >= 0.33)} else {filter(freqMAD3_gain2 >=0.3 )} %>% arrange(desc(N_MAD3)) Error in if (.) N_MAD3 < 9 else { : argument is not interpretable as logical In addition: Warning message: In if (.) N_MAD3 < 9 else { : the condition has length > 1 and only the first element will be used ---------------------------------------------------------------------- ------------------------------------------------------------ -SECURITY/CONFIDENTIALITY WARNING- This message and any attachments are intended solely for the individual or entity to which they are addressed. This communication may contain information that is privileged, confidential, or exempt from disclosure under applicable law (e.g., personal health information, research data, financial information). Because this e-mail has been sent without encryption, individuals other than the intended recipient may be able to view the information, forward it to others or tamper with the information without the knowledge or consent of the sender. If you are not the intended recipient, or the employee or person responsible for delivering the message to the intended recipient, any dissemination, distribution or copying of the communication is strictly prohibited. If you received the communication in error, please notify the sender immediately by replying to this message and deleting the message and any accompanying files from your system. If, due to the security risks, you do not wish to receive further communications via e-mail, please reply to this message and inform the sender that you do not wish to receive further e-mail from the sender. (LCP301)
The error below was fairly clear. The R 'if" statement is not vectorized and takes a single logical argument. It is not normally used in a pipeline unless at that point the data has been reduced to a vector of length 1. I do not want to look at your code further without the data behind it but suggest there is a vectorized ifelse() function that might fit your needs but since you use filter(0 so many times in there, maybe this should not be one long pipeline. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Yuan Chun Ding via R-help Sent: Tuesday, October 26, 2021 6:39 PM To: r-help at r-project.org Subject: [R] how to include if conditions in dplyr filter function Hi R users, I thought the follow R code should work, but I got error, Can you fix my code? Thank you, Ding outlier_tcga_MAD3 <- outlier_tcga %>% filter(n_two >0) %>% mutate(freqMAD3_gain2ratio = N_MAD3_gain2/n_two )%>% if (N_MAD3 < 9) {filter(freqMAD3_gain >=1)} else if (N_MAD3 > n_two*2 ) {filter (freqMAD3_gain >= 0.8 & freqMAD3_gain2ratio >0.33)} else {filter(freqMAD3_gain2 >=0.3 )} %>% arrange(desc(N_MAD3)) Error in if (.) N_MAD3 < 9 else { : argument is not interpretable as logical In addition: Warning message: In if (.) N_MAD3 < 9 else { : the condition has length > 1 and only the first element will be used ---------------------------------------------------------------------- ------------------------------------------------------------ -SECURITY/CONFIDENTIALITY WARNING- This message and any attachments are intended solely for the individual or entity to which they are addressed. This communication may contain information that is privileged, confidential, or exempt from disclosure under applicable law (e.g., personal health information, research data, financial information). Because this e-mail has been sent without encryption, individuals other than the intended recipient may be able to view the information, forward it to others or tamper with the information without the knowledge or consent of the sender. If you are not the intended recipient, or the employee or person responsible for delivering the message to the intended recipient, any dissemination, distribution or copying of the communication is strictly prohibited. If you received the communication in error, please notify the sender immediately by replying to this message and deleting the message and any accompanying files from your system. If, due to the security risks, you do not wish to receive further communications via e-mail, please reply to this message and inform the sender that you do not wish to receive further e-mail from the sender. (LCP301) ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.