0

I am wondering how can I do this thing work:

features_windows <- function(data, window_size) {
  data_with_feature <- data %>%
    mutate(window_one = nrow(data[data$ID<ID & data$key==key & data$ID > ID - window_size,]))
   data_with_feature
}

Basically the new feature should be how much points are there in the time-window ID-window_size : ID with the key. Currently I am getting 0s everywhere and for loop is a terrible idea in R. Any ideas?

Regards, T

Todor Kostov
  • 107
  • 1
  • 1
  • 10
  • how about adding `ID` and `key` in your function? i.e. `features_windows <- function(data, ID, key, window_size) { ...` – Adam Quek Apr 30 '17 at 01:10
  • I am using the mutate function of dplyr, wich takes the ID and key of the row where the new value is computed. – Todor Kostov Apr 30 '17 at 01:15
  • You are getting zeroes because `nrow( some-number )` will always be zero. nrow is for data frames; you do not have a data frame in there. – Pierre L Apr 30 '17 at 02:02
  • You should also check the logic for `window_one`. data$ID can't be less than ID AND greater than ID at the same time. So that will also always return zero. If you meant to evaluate `data$ID > ID - window_size` add a few separators. `data$ID < ID & data$ID > (ID - window_size) & ...` – Pierre L Apr 30 '17 at 02:07
  • Just checked with nrow(data[data$lineID < lineID,] - still zeroes. If I run only this part of the code as nrow(data[data$lineID < 500,] for example the result is 16... – Todor Kostov Apr 30 '17 at 02:11
  • Your example is not complete. We cannot copy the code and run it on our systems to test the problem and possible solutions. IF you would like to learn more about reproducible examples, see [Great Reproducible Question](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Pierre L Apr 30 '17 at 02:13
  • Thanks, I will review it. I also found the solution by myself and will post it shortly with the addition of making it reproducible. – Todor Kostov Apr 30 '17 at 02:17

1 Answers1

0

Found the solution:

use the group_by operator at the beginning:

features_windows <- function(data) {
  data_with_feature <- data %>%
    group_by(1:n()) %>%
    mutate(window_1 = nrow(data[data$ID < ID & data$ID > (ID - 10) & data$key == key, ]) ) %>%
  data_with_feature
Todor Kostov
  • 107
  • 1
  • 1
  • 10