简体   繁体   English

R-错误“可变长度不同”

[英]R - error “variable lengths differ”

> #transforming length of time
> transLOT<-log(LengthofTimemin)
> 
> #checking for outliers
> fit<-lm(transLOT~DielEnd+TideEnd+TideStart+Moonphase+TideStart*Moonphase, data=resdata)
> outlierTest(fit)
    rstudent unadjusted p-value Bonferonni p
295 4.445284         1.1025e-05    0.0052808
> 
> #getting rid of the outlier data in row 295
> rdata<-resdata[-295, ]
> print(rdata[294:296,5:10])
# A tibble: 3 × 6
  DepartureDate       DepartureTime        LengthofTime LengthofTimemin EventLengthCategories
         <dttm>              <dttm>              <dttm>           <dbl>                 <chr>
1    2016-09-19 1899-12-30 23:46:46 1899-12-30 00:05:49        5.816667                  5-15
2    2016-09-20 1899-12-30 01:55:28 1899-12-30 00:01:20        1.333333                    <5
3    2016-09-20 1899-12-30 04:07:28 1899-12-30 00:01:21        1.350000                    <5
> newfit<-lm(transLOT~DielEnd+TideEnd+TideStart+Moonphase+TideStart*Moonphase, na.action=na.exclude, data=rdata)
Error in model.frame.default(formula = transLOT ~ DielEnd + TideEnd +  : 
  variable lengths differ (found for 'DielEnd')
> #now all of a sudden the variable lengths differ

I understand that the problem occurs with the removal of the row of data but I assumed that na.exclude would account for it. 我知道问题出在删除数据行上,但是我认为na.exclude会解决这个问题。 After thoroughly searching, I am unable to determine why this error is occurring. 彻底搜索后,我无法确定为什么会发生此错误。

This happens because in your first step you created a separate variable outside of your data frame, transLOT<-log(LengthofTimemin) . 发生这种情况是因为,在第一步中,您在数据框之外创建了一个单独的变量transLOT<-log(LengthofTimemin) When you remove a row from the data, transLOT is unchanged. 从数据中删除一行时, transLOT保持不变。 Even worse than differing lengths, your data doesn't line up any more - if the different lengths were ignored, your rows of data would be "off by one" compared to the response after the row you removed. 甚至比不同的长度还差,您的数据不再排成一行-如果忽略了不同的长度,则与删除后的响应相比,数据行将“偏离一”。

The simple solution is to create your transLOT variable in the data frame. 简单的解决方案是在数据框中创建您的transLOT变量。 Then, whenever you do things to the data (like remove rows), the same thing is done to transLOT . 然后,无论何时对数据执行操作(例如删除行),都对transLOT进行相同的操作。

resdata$transLOT <- log(resdata$LengthofTimemin)

Note that I also use the resdata$LengthofTimemin rather than LengthofTimemin which you seem to have in your workspace. 请注意,我还使用resdata$LengthofTimemin而不是LengthofTimemin你似乎在您的工作空间。 Did you use attach() at some point? 您是否曾经使用过attach() You shouldn't use attach for exactly this reason. 正是由于这个原因,您不应该使用attach Keep variables in the data frame! 将变量保留在数据框中!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM