sqldf语句删除行语法错误?

时间:2012-12-10 15:16:18

标签: r sqldf

您好stackoverflow专家,

我正在尝试使用sqldf删除r表中的行,如下所示:

structure(list(Similarity = c(999L, 888L, 756L, 879L, 567L, 567L), Peak = c(797L, 833L,999L, 798L, 834L, 444L), Formula = structure(c(4L,3L, 4L, 1L, 2L, 2L), .Label = c("C12H26S", "C16H19NO", "C2H8O2Si","C9H13NO2"), class = "factor")), .Names = c("Similarity", "Peak","Formula"), class = "data.frame", row.names = c(NA, -6L))

我的目标是删除公式列中出现“SI”的行,如下所示:

structure(list(Similarity = c(999L, 756L, 879L, 567L, 567L), 
Peak = c(797L, 999L, 798L, 834L, 444L), Formula = structure(c(3L, 
3L, 1L, 2L, 2L), .Label = c("C12H26S", "C16H19NO", "C9H13NO2"
), class = "factor")), .Names = c("Similarity", "Peak", "Formula"
), class = "data.frame", row.names = c(NA, -5L))

我尝试了一个sqldf语句:

sqldf("DELETE * FROM PO_raw WHERE Formula='Si'")

但是,当然,它没有用,因为没有只有“Si”的行。我是sqldf的初学者,在我看来,这可能是语法错误。我在网上搜索但没有找到任何例子。

有什么建议吗?甚至可以使用sqldf吗?

欢呼声,

旧金山

1 个答案:

答案 0 :(得分:4)

使用LIKE和通配符,如下所示:

> sqldf(c("DELETE FROM PO_raw WHERE Formula like '%Si'", "select * from main.PO_raw"))
  Similarity Peak  Formula
1        999  797 C9H13NO2
2        756  999 C9H13NO2
3        879  798  C12H26S
4        567  834 C16H19NO
5        567  444 C16H19NO

(顺便说一句:有趣的question about LIKE