简体   繁体   English

错误为“如果(x == 1){…,则条件的长度> 1,并且仅使用第一个元素”

[英]Error of “In if (x == 1) { … : the condition has length > 1 and only the first element will be used”

I'm trying to calculate the mean and sd of certain columns in a data.frame. 我正在尝试计算data.frame中某些列的平均值和sd。 I run the following loop: 我运行以下循环:

calculations <- by(dataset, dataset$id, function (x) 
{
  if(x == 1) 
  {
    c(mean(x$Var1),mean(x$Var2))
    print("Cannot take sd, number of obs is equal to 1")
  }
  else(x > 1)
  {
    c(mean(x$Var1), mean(x$Var2), sd(x$Var1), sd(x$Var2))
  }
  #return(c(mean(x$Var1), mean(x$Var2), sd(x$Var1), sd(x$Var2))) 
})

and I get an output of: 我得到的输出是:

dataset$id: 1
[1] 0.3961182 3.8605641 1.0251303 2.6779033
-------------------------------------------------------------------------- 
dataset$id: 2
[1] 0.1656521 3.7565732 0.8687900 2.2305298
-------------------------------------------------------------------------- 
dataset$id: 3
[1] -0.3831954  4.0803145  1.3875692  2.1146944
-------------------------------------------------------------------------- 
dataset$id: 4
[1] 0.6719857 4.7523648 0.2001029 1.3715562
-------------------------------------------------------------------------- 
dataset$id: 5
[1] 0.01666328 3.18141270 0.98473329 1.76379804
-------------------------------------------------------------------------- 
dataset$id: 6
[1] 0.2542346 4.6464406 1.1077001 2.4604031
-------------------------------------------------------------------------- 
dataset$id: 7
[1] -0.1826018  5.6737908         NA         NA

up to dataset$id 40. with my loop I want the NA's to print off "Cannot take sd." 直到dataset $ id40。在我的循环中,我希望NA打印“无法取sd”。 When I run my code I just end up with the following error message: 当我运行代码时,我只会得到以下错误消息:

Warning messages:
1: In if (x == 1) { ... :
  the condition has length > 1 and only the first element will be used
2: In if (x == 1) { ... :
  the condition has length > 1 and only the first element will be used
3: In if (x == 1) { ... :
  the condition has length > 1 and only the first element will be used

Does anyone know how to fix this? 有谁知道如何解决这一问题?

It is clear from 很明显从

by(dataset, dataset$id, function (x) ....

that x takes subsets of rows of datatset . x接受datatset行的子集。 Your code then says 然后您的代码说

if (x == 1)

and as x is a data frame what do you hope to achieve by testing if the data frame is equal to 1? 并且由于x是数据帧,您希望通过测试数据帧是否等于1来实现什么?

If you are testing for the number of rows then consider 如果要测试行数,请考虑

if (nrow(x) > 1L)

as the test and flip the content of the if and else branches or do 作为测试并翻转ifelse分支或做的内容

if (nrow(x) < 2L) ## or
if (nrow(x) == 1L)

if you don't want to flip the branches. 如果您不想翻转分支。

暂无
暂无

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

相关问题 “条件的长度&gt; 1,并且仅使用第一个元素”错误 - “the condition has length > 1 and only the first element will be used” error 错误:条件的长度&gt; 1,并且在r中仅使用第一个元素 - Error: condition has length > 1 and only the first element will be used in r R if 语句错误:条件长度 &gt; 1 且仅使用第一个元素 - R if statement error: the condition has length > 1 and only the first element will be used 错误:“条件长度 &gt; 1,仅使用第一个元素” - Error: “the condition has length > 1 and only the first element will be used” 条件的长度 &gt; 1 并且只使用第一个元素错误 - the condition has length > 1 and only the first element will be used error 如果语句中出现%in%错误条件警告:长度&gt; 1,并且仅使用第一个元素 - If statement with %in% error Condition warning: has length > 1 and only the first element will be used if 函数中的错误:条件的长度 &gt; 1,并且只会使用第一个元素 - Error in the if function: the condition has length > 1 and only the first element will be used 条件的长度&gt; 1,并且仅使用第一个元素 - The condition has length > 1 and only the first element will be used 如果返回“条件的长度大于1,并且将仅使用第一个元素” - IF returns “the condition has length > 1 and only the first element will be used” 条件的长度&gt; 1,并且仅使用第一个元素(RE:miceadds,MIDS) - the condition has length > 1 and only the first element will be used (RE: miceadds, MIDS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM