如何在Excel或R中的多个单元格中的单个单元格内分离数据

时间:2018-07-26 16:26:38

标签: r excel-formula

状态更新信息

"Placed | 2018-06-23 08:46:46 Acknowledged | 2018-06-23 08:53:03 Dispatched | 2018-06-23 09:02:54 Completed | 2018-06-23 09:03:12"
"Placed | 2018-06-23 09:03:28 Acknowledged | 2018-06-23 09:03:43 Dispatched | 2018-06-23 09:06:31 Completed | 2018-06-23 09:06:46"

我想要以上数据无关列: 一栏应该有 放置/确认/发送/完成以及其他应具有日期和时间。

我们如何在Excel和R中做到这一点?

1 个答案:

答案 0 :(得分:0)

在R

library(tidyr)
library(dplyr)

df %>% mutate(A = gsub("Placed\\s+\\||Acknowledged|Dispatched|Completed","",A)) %>% 
       separate(A,c('Placed',"Acknowledged","Dispatched","Completed"),'\\|') %>% 
       mutate_all(funs(trimws(., which = 'both')))

         Placed        Acknowledged          Dispatched           Completed
1 2018-06-23 08:46:46 2018-06-23 08:53:03 2018-06-23 09:02:54 2018-06-23 09:03:12
2 2018-06-23 09:03:28 2018-06-23 09:03:43 2018-06-23 09:06:31 2018-06-23 09:06:46

数据

df <- read.table(text="
                     A
                    'Placed | 2018-06-23 08:46:46 Acknowledged | 2018-06-23 08:53:03 Dispatched | 2018-06-23 09:02:54 Completed | 2018-06-23 09:03:12'
                    'Placed | 2018-06-23 09:03:28 Acknowledged | 2018-06-23 09:03:43 Dispatched | 2018-06-23 09:06:31 Completed | 2018-06-23 09:06:46'
                     ",header=T, stringsAsFactors = F)