简体   繁体   English

R:如何在 if 语句中设置 2 个结果

[英]R: how can I set 2 outcome in if statement

I want to set 2 Console output in the if statement: one is the paste() function and sencond is unique values from one of the column我想在 if 语句中设置2 个控制台 output :一个是 paste() function,第二个是来自其中一列的唯一值

if (x<= 0) {
Paste("Negative"),
unique(data$category)
}

You can't return two things at once, but there are couple of ways to output both objects on the console:你不能一次返回两个东西,但是有几种方法可以在控制台上 output 两个对象:

  1. Assign each object to a variable, and output the variables one at a time将每个 object 分配给一个变量,并将 output 一次分配一个变量
  2. Store both objects together in a list, and output the list to the console将两个对象一起存储在一个列表中,并将 output 列表到控制台

1. Assign each value to a different variable 1.将每个值分配给不同的变量

The variables will be stored in the global environment, so they will be accessible subsequent to the execution of the if statement.变量将存储在全局环境中,因此在执行 if 语句后可以访问它们。

x <- -3
data <- data.frame(category = c("foo", "bar", "baz"))

if (x <= 0){
  my.text <- "Negative"
  my.values <- unique(data[["category"]])
}
 
## Now you can do as you please with these variables. For example:

cat("The value of x is", my.text)
#> The value of x is Negative

length(my.values)
#> [1] 3

Created on 2022-08-23 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 8 月 23 日创建

2. Immediately return a two-element list 2.立即返回一个二元素列表

x <- -3
data <- data.frame(category = c("foo", "bar", "baz"))


if (x <= 0){

  list(
    polarity = "Negative",
    values   = unique(data[["category"]])
  )

}
#> $polarity
#> [1] "Negative"
#> 
#> $values
#> [1] "foo" "bar" "baz"

Created on 2022-08-23 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 8 月 23 日创建

3. Bonus solution: Use cat() to print to the console 3. 奖励解决方案:使用cat()打印到控制台

If you don't actually want to return the objects, but you just want to print them to the console, you can use cat() .如果您实际上不想返回对象,而只想将它们打印到控制台,则可以使用cat() Depending on your aims, you might also consider using message() , which is designed to give messages to the user (or a log).根据您的目标,您还可以考虑使用message() ,它旨在向用户(或日志)提供消息。

x <- -3
data <- data.frame(category = c("foo", "bar", "baz"))


if (x <= 0){

  cat("Negative")
  cat(data[["category"]])
  
}
#> Negativefoo bar baz

### or consider:

if (x <= 0){
  
  message("Negative")
  message(data[["category"]])
  
}
#> Negative
#> foobarbaz

Created on 2022-08-23 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 8 月 23 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何整理粘贴 function 的结果? (右) - How can I tidy the outcome of paste function? (R) 如何将具有二元结果比率的数据集转换为适合R中逻辑回归的数据集 - How can i convert a dataset with ratios for a binary outcome to something suitable for logistic regression in R 如何计算 R 中给定概率集的 brier 分数以获得生存结果? - How to calculate brier score for a given set of probabilities in R for survival outcome? 有没有可以用来在R中获得目标结果规则的软件包? - Is there a package that I can use in order to get rules for a target outcome in R 我可以根据R Cubist的结果制作图表吗? - Can I make a diagram out of R Cubist outcome? 如何在R中将错误用作if语句 - How can I use an error as an if statement in R 如何在R中编写switch语句? - How can I write a switch statement in R? 如何在 R 中获得这个概率结果? - How to get this probability outcome in R? 如果我有一个想要修改和更改其值的现有列,我如何使用 R 设置条件语句? - If i have an existing column that i want to modify and change its values, how can i set a conditional statement using R? 如何仅为一种结果类型计算新数据框? - How can I calculate a new dataframe only for one outcome type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM