5

I i am using the following code to increase the next observation by the previous observation * .5:

abc <- data.frame(1:6)
abc$b <- 0
colnames(abc) <- c ("imps","Effect")  

abc$Effect <- filter(abc$imps, filter = 0.5, method = "recursive")

I keep getting the error:

Error in UseMethod("filter_") : 
  no applicable method for 'filter_' applied to an object of class "c('integer', 'numeric')"

The desired output:

imps    Effect
1   1
2   2.5
3   4.25
4   6.125
5   8.0625
6   10.03125

Any suggestions? NOTE: Yesterday before I installed 3.2.5 from 3.2.2 this ran just fine

cesco
  • 63
  • 1
  • 1
  • 4

1 Answers1

4

You can also use Reduce to do what you want:

abc$Effect = Reduce(function(i,j) j+i*0.5,abc$imps,accumulate=T)
  imps   Effect
1    1  1.00000
2    2  2.50000
3    3  4.25000
4    4  6.12500
5    5  8.06250
6    6 10.03125

Your issue can come from the fact that the dplyr library is masking the filter function, in which case, you should precise stats::filter(..).

Lamia
  • 3,845
  • 1
  • 12
  • 19
  • Thanks a lot; both solutions work. So, let me ask you a question: what does it mean when a library is "masking" a function in layman's terms and how do you avoid it? – cesco Jul 03 '17 at 16:38
  • 1
    There is a function called `filter` in the `dplyr` package, which is different from the `filter` function from base. When you load the `dplyr` package, it warns you that certain functions will be masked, one of them being `filter` from the `stats` package, meaning that from then on, when calling `filter` it's the `dplyr` one that will be called instead of the base one. I am not aware of a way to prevent a package masking certain functions. – Lamia Jul 03 '17 at 19:18
  • But there are a few workarounds. See [question1](https://stackoverflow.com/questions/26935095/r-dplyr-filter-not-masking-base-filter#26935536) and [question2](https://stackoverflow.com/questions/39137110/what-does-the-following-object-is-masked-from-packagexxx-mean). – Lamia Jul 03 '17 at 19:18