sqldf - изменить синтаксис для добавления нового столбца во фрейм данных - PullRequest
0 голосов
/ 16 октября 2018

Я хочу добавить новое поле во фрейм данных на sqldf.

> data(mtcars)
> library(sqldf)
> sqldf("alter table mtcars add newfield  int not null")
Error in result_create(conn@ptr, statement) : 
  Cannot add a NOT NULL column with default value NULL
> 
> sqldf("alter table mtcars add newfield  int")
data frame with 0 columns and 0 rows
Warning message:
In result_fetch(res@ptr, n = n) :
  Don't need to call dbFetch() for statements, only for queries

Я получаю пустой фрейм данных из последней команды.Выражение sql кажется нормальным.Но я не знаю, что происходит не так.

1 Ответ

0 голосов
/ 16 октября 2018

Мы можем добавить новый столбец, используя sqldf (я не уверен насчет добавления столбца не null):

data(mtcars)
library(sqldf)
sqldf(c("alter table mtcars add newfield numeric","select * from mtcars"))

Пример вывода:

     mpg cyl  disp  hp drat    wt  qsec vs am gear carb newfield
1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4       NA
2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4       NA
3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1       NA
4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1       NA
...