在 R 中使用 apply() 时未使用的参数错误 [英] unused arguments error using apply() in R

查看:64
本文介绍了在 R 中使用 apply() 时未使用的参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试以日期列为条件使用 apply() 返回一组系数时收到错误消息.

I get an error message when I attempt to use apply() conditional on a column of dates to return a set of coefficients.

我有一个数据集(此处为简单起见进行了修改,但可重现):

I have a dataset (herein modified for simplicity, but reproducible):

ADataset <- data.table(Epoch = c("2007-11-15", "2007-11-16", "2007-11-17", 
                       "2007-11-18", "2007-11-19", "2007-11-20", "2007-11-21"),
                       Distance = c("92336.22", "92336.23", "92336.22", "92336.20",
                       "92336.19", "92336.21", "92336.18))
ADataset
        Epoch Distance
1: 2007-11-15 92336.22
2: 2007-11-16 92336.23
3: 2007-11-17 92336.22
4: 2007-11-18 92336.20
5: 2007-11-19 92336.19
6: 2007-11-20 92336.21
7: 2007-11-21 92336.18

分析从确定开始和结束日期开始:

The analysis begins with establishing start and end dates:

############## Establish dates for analysis
#4.Set date for center of duration
StartDate <- "2007-11-18"
as.numeric(as.Date(StartDate)); StartDate
EndDate <- as.Date(tail(Adataset$Epoch,1)); EndDate

然后我确定分析的持续时间:

Then I establish time durations for analysis:

#5.Quantify duration of time window
STDuration <-  1
LTDuration  <- 3

然后我编写函数来回归两个持续时间并返回斜率:

Then I write functions to regress over both durations and return the slopes:

# Write STS and LTS functions, each with following steps
#6.Define time window- from StartDate less ShortTermDuration to 
StartDate plus ShortTermDuration
#7.Define Short Term & Long Term datasets
#8. Run regression over dataset
my_STS_Function <- function (StartDate) {

  STAhead  <- as.Date(StartDate) + STDuration; STAhead
  STBehind <- as.Date(StartDate) - STDuration; STBehind
  STDataset  <- subset(Adataset, as.Date(Epoch) >= STBehind & as.Date(Epoch)<STAhead)
  STResults <- rlm( Distance ~ Epoch, data=STDataset); STResults
  STSummary <- summary( STResults ); STSummary
  # Return coefficient (Slope of regression)
  STNum <- STResults$coefficients[2];STNum
}
my_LTS_Function <- function (StartDate) {
  LTAhead  <- as.Date(StartDate) + LTDuration; LTAhead
  LTBehind <- as.Date(StartDate) - LTDuration; LTBehind
  LTDataset  <- subset(Adataset, as.Date(Epoch) >= LTBehind & as.Date(Epoch)<LTAhead)
  LTResults <- rlm( Distance ~ Epoch, data=LTDataset); LTResults
  LTSummary <- summary( LTResults ); LTSummary
  # Return coefficient (Slope of regression)
  LTNum <- LTResults$coefficients[2];LTNum

然后我测试该函数以确保它适用于单个日期:

Then I test the function to make sure it works for a single date:

myTestResult <- my_STS_Function("2007-11-18")

它有效,所以我继续在数据集中的日期范围内应用该函数:

It works, so I move on to apply the function over the range of dates in the dataset:

mySTSResult <- apply(Adataset, 1, my_STS_Function, seq(StartDate : EndDate))

...其中我想要的结果是 mySTSResult(斜率)的列表或数组或向量(以及随后的 myLTSResults 的单独列表/数组/向量,这样我就可以在持续时间内创建 STSlope:LTSlope 比率),类似(mySTSResults 捏造)...

...in which my desired result is a list or array or vector of mySTSResult (slopes) (and, subsequently, a separate list/array/vector of myLTSResults so then I can create a STSlope:LTSlope ratio over the duration), something like (mySTSResults fabricated)...

> Adataset
    Epoch Distance mySTSResults
1: 2007-11-15 92336.22            3
2: 2007-11-16 92336.23            4
3: 2007-11-17 92336.22            5
4: 2007-11-18 92336.20            6
5: 2007-11-19 92336.19            7
6: 2007-11-20 92336.21            8
7: 2007-11-21 92336.18            9

只有我收到此错误:

Error in FUN(newX[, i], ...) : unused argument(s) (1:1185)

这告诉我什么以及如何纠正它?我做了一些查找,但找不到更正.

What is this telling me and how to do correct it? I've done some looking and cannot find the correction.

希望我已经充分解释了这一点.如果您需要更多详细信息,请告诉我.

Hopefully I've explained this sufficiently. Please let me know if you need further details.

推荐答案

添加这个作为一个新的答案,因为上一个变得混乱.之前的评论者是正确的,您的代码中存在错误,但它们不是症结所在.

Adding this as a new answer as the previous one was getting confused. A previous commenter was correct, there are bugs in your code, but they aren't a sticking point.

我更新的方法是使用 seq.Date 生成日期序列(仅当您在开始和结束之间的每一天都有数据点时才有效 - 尽管您可以使用 na.exclude 如上所述):

My updated approach was to use seq.Date to generate the date sequence (only works if you have a data point for each day between the start and end - though you could use na.exclude as above):

dates = seq.Date(as.Date(StartDate),as.Date(EndDate),"days")

然后您将其用作要应用的输入,并进行一些类型调整以使事情正常工作(我已使用 lamda 函数完成此操作):

You then use this as the input to apply, with some munging of types to get things working correctly (I've done this with a lamda function):

mySTSResult <- apply(as.matrix(dates), 1, function(x) {class(x) <- "Date"; my_STS_Function(x)})

然后希望您应该有一个结果向量,并且您应该能够为 LTS 做类似的事情,然后将其处理到原始数据框/矩阵中的另一列中.

Then hopefully you should have a vector of the results, and you should be able to do something similar for LTS, and then manipulate that into another column in your original data frame/matrix.

这篇关于在 R 中使用 apply() 时未使用的参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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