[英]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 两个对象:
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 日创建
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 日创建
cat()
to print to the console 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.