R错误:“在数值表达式中有19个元素:只有第一个使用”。 [英] R Error: "In numerical expression has 19 elements: only the first used"

查看:797
本文介绍了R错误:“在数值表达式中有19个元素:只有第一个使用”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个数据框:

totalDeposit <- cumsum(testd$TermDepositAMT[s1$ix])

基本上是计算被测试数据帧中TermDeposit金额的累积总和并将其存储在总存款中。

which is basically calculating cumulative sum of TermDeposit amounts in testd dataframe and storing it in total deposit. This works perfrectly ok.

然后我需要计算存款金额的平均值,然后使用以下内容:

I then need to calculate the average of the deposit amount and i use the following:

avgDeposit <- totalDeposit / (1:testd)

但收到错误消息

   Error in 1:testd : NA/NaN argument
In addition: Warning message:
In 1:testd : numerical expression has 19 elements: only the first used

tested具有大约8000个观测值和19个变量。

testd has some 8000 observations and 19 variables.

有人可以帮我解决这个问题吗?我试图在线查找此错误消息,但到目前为止,我所了解的只是:1:tested基本上使R读取了testd作为不是的数字,因此我得到了一条错误消息。会简单地采取mean(totalDeposit)的技巧吗?我尝试过,但是我得到的数字是荒谬的,无处代表平均值。

Could someone help me get past this issue? I've attempted to locate this error message online but all i've understood so far is that 1:testd basically makes R read testd as a number which it isn't and hence i get an error message. Would simply taking mean(totalDeposit) do the trick? I tried it but the figure i get is absurd and nowhere representative of the average.

谢谢您的帮助。

推荐答案

在这种情况下,错误消息很有帮助。

The error message is, in this case, helpful.

当您说 1时: N ,您告诉R是给我1到N之间的整数序列。从整数1到整数2。 testd 不是整数,(最好)是整数的整个向量,因此R会处理除 testd <中的第一个值以外的所有值。 / code>计算序列时。替代方案可能是可怕的错误,也可能是序列的 set -一个介于1和 testd 中的第一个值之间,另一个介于1和第二个之间 tested ...中的值,依此类推。

When you say 1:N, what you're telling R is "give me the sequence of integers between 1 and N". It's from integer1 to integer2. testd isn't an integer, it's (at best) an entire vector of integers, and so R disposes of all but the first value in testd when calculating the sequence. The alternative would be either a horrible error or a set of sequences - one between 1 and the first value in testd, another between 1 and the second value in testd...and so on.

您想要的是 1 :nrow(testd),如果testd是数据帧,并且 1:length(testd) seq_along(已测试)是列表还是向量。

What you want instead is 1:nrow(testd), if testd is a data frame, and either 1:length(testd) or seq_along(testd) if it's a list or vector.

不过,根据问题-是否需要计算平均值? -您实际上正在解决这个错误,因为您不想要一个值序列,而只想要一个:由于average =总数/进入该总数的元素数量,您只需要'元素数量'-可以使用 nrow(tested)进行检索。

Based on the question, though - the need to calculate averages? - you're actually approaching this wrong, because you don't want a sequence of values, you just want one: since average = total/number of elements that went into that total, you just want 'the number of elements' - which can be retrieved simply with nrow(testd).

这篇关于R错误:“在数值表达式中有19个元素:只有第一个使用”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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