如何在r中显示消息? [英] How to display message in r?

查看:249
本文介绍了如何在r中显示消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,根据某些条件显示为false(请参见下文)。如果所有单元格中出现'false',我想显示一条消息(例如,失败)

i have a dataframe which displays false (please see below) based on certain conditions. I would like to display a message (say, fail) if 'false' appears in all the cells

    a   b   c
1   FALSE   FALSE   FALSE
2   FALSE   FALSE   FALSE
3   FALSE   FALSE   FALSE

i尝试下面的代码,但它不起作用

i tried the code below, but it doesn't work

if (dataframe_name=="false")
print(fail)


推荐答案

这是另一种需要考虑的方法。我已将它包括在内(1)向您展示消息函数,(2)向您展示如何创建一个可重复性最小的示例,(3)如何编写一个简单的功能,以及(4)提供问题的工作答案,因为现有答案不正确。

Here is an alternative to consider. I've included it to (1) show you the "message" function, (2) show you how to create a minimal reproducible example, (3) how to write a simple function, and (4) provide a working answer to your question since the existing answer is incorrect.

df1 <- data.frame(a = c(FALSE, FALSE), b = c(FALSE, FALSE))
df2 <- data.frame(a = c(FALSE, TRUE), b = c(FALSE, FALSE))

checkMe <- function(inDF) {
  if(!any(inDF == TRUE)) message("false") 
  else message(paste0((sum(inDF == TRUE)/prod(dim(inDF)))*100, "% true values"))
  inDF
}

checkMe(df1)
# false
#       a     b
# 1 FALSE FALSE
# 2 FALSE FALSE
checkMe(df2)
# 25% true values
#       a     b
# 1 FALSE FALSE
# 2  TRUE FALSE

这篇关于如何在r中显示消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆