使用sqldf按分隔符进行解析

时间:2019-03-07 22:56:07

标签: r sqldf

我有一个如下数据框:

df
   ColA    ColB
  New|ME      7
 New|You      8
     ten      9
Aim|High     10

预期输出:

   ColA    ColB    First   Second
  New|ME      7      New       ME
 New|You      8      New      You
     ten      9      ten       NA
Aim|High     10      Aim     High

如何通过定界符|使用sqldf解析ColA?

这是我的尝试,但收到错误:

library(sqldf)
df1<- sqldf("SELECT * FROM df CROSS APPLY STRING_SPLIT([ColA], '|') ")

Error in result_create(conn@ptr, statement) : near "(": syntax error

1 个答案:

答案 0 :(得分:0)

df <- sqldf("SELECT *, SUBSTR([ColA], 1, INSTR([ColA],'|')-1) First FROM df")
df <- sqldf("SELECT *, SUBSTR([ColA], INSTR([ColA],'|')+1, Length([ColA])) Second FROM df")
df <- sqldf("SELECT ColA, ColB, First, Second FROM df")