根据分隔符拆分列并创建新列

时间:2019-04-19 15:37:01

标签: r

需要帮助根据不同的分隔符来拆分列 使用多个定界符时遇到问题 以下是样本数据和预期输出。

样本数据

ContractCd <- c(9940099251,9940080497,9940099251,9940014221)

WBSElementNbr <- c("N1075001,N1075013,MT842001,N1128001,NN480001,N1142001,N1147001","IV768001&IU775001","NN480001;N1147001","D6268001/D6268005")

Sample Data
Data <- data.frame(ContractCd,WBSElementNbr)

预期产量

ContractCd <- c(9940099251,
                9940099251,
                9940099251,
                9940099251,
                9940099251,
                9940099251,
                9940099251,
                9940080497,
                9940080497,
                9940099251,
                9940099251,
                9940014221,
                9940014221)

WBSElementNbr <- c("N1075001",
                   "N1075013",
                   "MT842001",
                   "N1128001",
                   "NN480001",
                   "N1142001",
                   "N1147001",
                   "IV768001",
                   "IU775001",
                   "NN480001",
                   "N1147001",
                   "D6268001",
                   "D6268005")

Expected Output
Data1 <- data.frame(ContractCd,WBSElementNbr)

1 个答案:

答案 0 :(得分:1)

我们可以使用separate_rows

library(tidyverse)
Data %>%
   separate_rows(WBSElementNbr)