Francisco
Francisco

Reputation: 391

sqldf statement delete rows syntax error?

Hi stackoverflow experts,

I´m trying to use sqldf to delete rows in a r table such as this:

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))

My goal is to delete rows where "SI" appears on the formula column like this:

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))

I have tried a sqldf statement:

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

But, of course, it doesn´t work since there is no row with only "Si". I´m a beginner with sqldf and it seems to me that this is probably an error in the syntax. I have search in the web but didn´t found any example.

Any suggestion? Is this even possible with sqldf?

cheers,

Francisco

Upvotes: 1

Views: 2116

Answers (1)

ROLO
ROLO

Reputation: 4223

Use LIKE and a wildcard, like this:

> 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

(BTW: Interesting question about LIKE)

Upvotes: 4

Related Questions