I have converted a dataframe intro sqlite database using rsqlite
package of R programming. First column of table sh3
is of type TEXT
with 23 strings separated by separater ' '. I want to convert this column into 23 columns and drop it afterwards. Till now I have added 23 columns with data type TEXT
and after that I have tried many things but failed.
library(DBI)
library(RSQLite)
# connecting to database
con <- dbConnect(SQLite(), dbname = 'ShearTest3.sqlite')
# Type of data in fields
dbGetQuery(con,"PRAGMA table_xinfo(sh3)")
# Adding 23 columns into table sh3 with TEXT format by using following code
dbExecute(con,"ALTER TABLE sh3 ADD COLUMN X1 TEXT")
dbExecute(con,"ALTER TABLE sh3 ADD COLUMN X2 TEXT")
dbExecute(con,"ALTER TABLE sh3 ADD COLUMN X3 TEXT")
dbExecute(con,"ALTER TABLE sh3 ADD COLUMN X4 TEXT")
...
# Things I have tried
# Rockable 29-11-2018 is the column name
dbGetQuery(con, "UPDATE sh3
SET X1=split_part(`Rockable 29-11-2018`, ' ',1),
X2=split_part(`Rockable 29-11-2018`, ' ',2),
X3=split_part(`Rockable 29-11-2018`, ' ',3),
X4=split_part(`Rockable 29-11-2018`, ' ',4),
...
Current format
dbGetQuery(con,"SELECT * FROM sh3 LIMIT 10 OFFSET 10000000")
+---------------------------------------------------------------------------------------------+---------+-------+-------+--------+-------+--------+-------+--------+------+--------+-------+--------+------+-------+-------+--------+-------+-------+--------+-------+-------+--------+-------+------+
| Rockable29-11-2018 | V1 | X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 | X9 | X10 | X11 | X12 | X13 | X14 | X15 | X16 | X17 | X18 | X19 | X20 | X21 | X22 | X23 |
+---------------------------------------------------------------------------------------------+---------+-------+-------+--------+-------+--------+-------+--------+------+--------+-------+--------+------+-------+-------+--------+-------+-------+--------+-------+-------+--------+-------+------+
| a b c w d z a john f kennedy barack obama george bush washington d c n y police is in there | conf261 | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> | <NA> |
+---------------------------------------------------------------------------------------------+---------+-------+-------+--------+-------+--------+-------+--------+------+--------+-------+--------+------+-------+-------+--------+-------+-------+--------+-------+-------+--------+-------+------+
and Format I want
+---------+-----+------+--------+-------+-------+-------+--------+-------+------+----------+---------+--------+---------+-------+--------------+-------+-------+-------+-------+--------+-------+-------+--------+
| V1 | X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 | X9 | X10 | X11 | X12 | X13 | X14 | X15 | X16 | X17 | X18 | X19 | X20 | X21 | X22 | X23 |
+---------+-----+------+--------+-------+-------+-------+--------+-------+------+----------+---------+--------+---------+-------+--------------+-------+-------+-------+-------+--------+-------+-------+--------+
| conf261 | a | b | c | w | d | z | a | john | f | kennedy | barack | obama | george | bush | washington | d | c | n | y | police | is | in | there |
+---------+-----+------+--------+-------+-------+-------+--------+-------+------+----------+---------+--------+---------+-------+--------------+-------+-------+-------+-------+--------+-------+-------+--------+