admin管理员组

文章数量:1599541

Warning message:In if (x > 1) { :the condition has length > 1 and only the first element will be used

 

目录

Warning message:In if (x > 1) { :the condition has length > 1 and only the first element will be used

问题:

解决:

完整问题:


 

问题:

#想把向量中元素大于1的乘以2;

#如下方法把所有的元素都乘以了2

#define data
x <- c(2, 3, 1, 1, 5, 7)

#if value in vector x is greater than 1, multiply it by 2
if (x>1) {
  x*2
}

[1]  4  6  2  2 10 14

解决:

#使用ifelse语法

#if value in vector x is greater than 1, multiply it by 2
ifelse(x>1, x*2, x)

[1]  4  6  1  1 10 14

完整问题:

> #define data
> x <- c(2, 3, 1, 1, 5, 7)

> #if value in vector x is greater than 1, multiply it by 2
> if (x>1) {
+     x*2
+ }
[1]  4  6  2  2 10 14
Warning message:
In if (x > 1) { :
  the condition has length > 1 and only the first element will be used

参考:R

参考:How to Fix in R: the condition has length > 1 and only the first element will be used

本文标签: Messagewarningconditionelementlength