Удалить список из столбца данных - PullRequest
1 голос
/ 12 февраля 2020

Имея этот фрейм данных

df <- structure(list(date = c("2008-07-31", "2008-08-04"), id = c(1L, 
                                                                  1L), body = list("text 2 and here another", 
                                                                                   c("another text here", 
                                                                                     "and this in the same row", 
                                                                                     "one more in the same row"
                                                                                   ))), row.names = 1:2, class = "data.frame")

Как можно отменить вывод столбца body, чтобы получить такой вывод:

 date id                                                                  body
1 2008-07-31  1                                               text 2 and here another
2 2008-08-04  1 another text here and this in the same row one more in the same row

Я пробовал это:

df$body <- as.data.frame(unlist(df$body))

Ответы [ 2 ]

1 голос
/ 12 февраля 2020

Вы можете использовать paste в sapply.

df$body <- sapply(df$body, paste, collapse = " ")
str(df$body)
# chr [1:2] "text 2 and here another" ...
1 голос
/ 12 февраля 2020

С dplyr и purrr вы можете сделать:

df %>%
 mutate(body = map_chr(body, paste, collapse = " "))

        date id                                                                body
1 2008-07-31  1                                             text 2 and here another
2 2008-08-04  1 another text here and this in the same row one more in the same row
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...