use string of character data to insert into table with multiple rows in Postgresql using R

Viewed 19

I get "data" from text file and here is what i get like chr[1:18].. below is my data. Need to convert it as a 6 column set and insert into database table.

str(data)
chr [1:18] "1" "4545" "1" "6" "4000" "Europe" "2" "3565" "5" "8" "5450" "Asia" "3" "4646" "1" "5" "3455" "Europe"

insert into mytable (column1, column2, column3, column4, column5, column6) 
values ( "1", "4545", "1", "6", "4000", "Europe"),
( "2", "3565", "5", "8", "5450", "Asia"),
("3", "4646", "1", "5", "3455", "Europe")

Could anyone suggest? I tried to google, but could not any good ones. Thanks.

1 Answers

Try this.

dat<-c("1", "4545", "1", "6", "4000", "Europe", "2", "3565", "5", "8", "5450", "Asia", "3", "4646", "1", "5", "3455", "Europe")
str(dat)
number_of_columns<-6

number_of_rows<-length(dat)/number_of_columns
data.frame(matrix(dat, nrow = number_of_rows))

The documentation for pushing the data to a database can be found at https://solutions.rstudio.com/db/databases/postgresql/

Related