Длина этикетки 2 должна быть 1 или 0 - PullRequest
1 голос
/ 06 января 2020

У меня есть эти данные:

library(dplyr)
dframefull <- data.frame(id = c(1,1,1,1,1,1,1,1), name = c("Google", "Google", "Google", "Google", "Google", "Google", "Google", "Google"), date = c("12/8/2014 19:30:57", "26/8/2014 19:30:57", "27/8/2014 10:12:01", "27/8/2014 14:10:29", "27/8/2014 14:10:32", "27/8/2014 14:10:33", "3/9/2014 14:10:32", "14/9/2014 19:30:57"), mytext = c("out text", "text", "another", "text", "here", "other text", "text more", "out text 2"))
dframekeep <- data.frame(id = c(1), name = c("Google"), date = c("27/8/2014 14:10:32"))

и выполнение этих команд

b <- with(dframefull, 
           aggregate(list(mytext=mytext), 
                     by=list(id=id, 
                             label=factor(I(date > dframekeep$date), labels=c("before", "after")), 
                             name=name), 
                     FUN=paste))

И я получаю эту ошибку:

Error in factor(I(date > dframekeep$date), labels = c("before", "after")) : 
  invalid 'labels'; length 2 should be 1 or 0
In addition: Warning message:
In Ops.factor(date, dframekeep$date) : ‘>’ not meaningful for factors

Любая идея, как я могу исправить это?

Ожидаемый результат - сохранить 10 дней до и 10 дней после отметки времени второго кадра данных с учетом секунд

data.frame(id = c(1,1), label = c("before", "after"), name = c("Google", "Google"), mytext = c("text another text here", "other text text more"))
  id  label   name                 mytext
1  1 before Google text another text here
2  1  after Google   other text text more

1 Ответ

1 голос
/ 06 января 2020

Я сделал два изменения в вашем коде. Сначала я использовал аргумент stringsAsFactors = FALSE в data.frame. Во-вторых, я преобразовал ваши date столбцы в класс POSIXct (класс даты / времени).

Это позволяет вашему примеру кода работать без ошибок. Я надеюсь, что решение в ваших производственных данных аналогично.

library(dplyr)
dframefull <- data.frame(id = c(1,1,1,1,1,1,1,1), 
                         name = c("Google", "Google", "Google", "Google", 
                                  "Google", "Google", "Google", "Google"), 
                         date = c("12/8/2014 19:30:57", "26/8/2014 19:30:57", 
                                  "27/8/2014 10:12:01", "27/8/2014 14:10:29", 
                                  "27/8/2014 14:10:32", "27/8/2014 14:10:33", 
                                  "3/9/2014 14:10:32",  "14/9/2014 19:30:57"), 
                         mytext = c("out text", "text", "another", "text", 
                                    "here", "other text", "text more", 
                                    "out text 2"),
                         stringsAsFactors = FALSE) %>% 
  mutate(date = as.POSIXct(date, 
                           format = "%d/%m/%Y %H:%M:%S"))
dframekeep <- data.frame(id = c(1), 
                         name = c("Google"), 
                         date = c("27/8/2014 14:10:32"),
                         stringsAsFactors = FALSE) %>% 
  mutate(date = as.POSIXct(date, format = "%d/%m/%Y %H:%M:%S"))

b <- with(dframefull, 
          aggregate(list(mytext=mytext), 
                    by=list(id=id, 
                            label=factor(I(date > dframekeep$date), labels=c("before", "after")), 
                            name=name), 
                    FUN=paste))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...