0

I realize that this is probably quite simple but the results i find online are far more complex than what I have and therefore lose me. in the process of learning R and at this time, creating a simple line chart. The data has values that I need filtered out prior to making the chart. I am looking for monthly data (periodtype = '03') and not annual (periodtype = '01'). Below is the code that i am using and the error that I receive. My data frame, labforce contains both periodtypes in it. Any thoughts on how I should go about this?

       con <- dbConnect(odbc::odbc(),"xyz-DB")
       > labforce1 <- dbGetQuery(con, "SELECT periodyear,period,unemprate 
       FROM labforce")


       labforce1m <- labforce1 %>%
       + filter (periodtype=='03')


       Error in UseMethod("filter_") : 
       no applicable method for 'filter_' applied to an object of class 
       "logical"

       dput(head(labforce1)) yields the following:

       structure(list(periodyear = c("1990", "1990", "1990", "1990", 
       "1990", "1990"), periodtype = c("03", "03", "03", "03", "03", 
       "03"), period = c("01", "02", "03", "04", "05", "06"), unemprate = 
       c(8.9, 
       8.8, 8.6, 7, 4.6, 5.8), date = structure(c(7305, 7336, 7364, 
       7395, 7425, 7456), class = "Date")), row.names = c(NA, 6L), class = 
       "data.frame")
       > 

      


       
Tim Wilcox
  • 1,275
  • 2
  • 19
  • 43
  • 1
    Can you add `labforce1` to your question? See: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Maximilian Peters Sep 25 '20 at 20:02
  • 1
    You are using the integer division operator `%/%` instead of the pipe operator `%>%`. Also check that `labforce1` is a data frame rather than a logical vector – Allan Cameron Sep 25 '20 at 20:03
  • 1
    If that is `dplyr::filter`, then do you mean to be using `%>%` instead of `%/%`? The latter is integer-division (not a "frame"-thing). – r2evans Sep 25 '20 at 20:04
  • 1
    It appears that `labforce1` is a `logical` vector, which means (1) it won't work with `dplyr`, and (2) it doesn't have any property named `periodtype`. – r2evans Sep 25 '20 at 20:04
  • I derive labforce1 by using odbc and taking stuff from a SQL Database. Does that make it a logical vector? – Tim Wilcox Sep 25 '20 at 20:07
  • 1
    It's difficult to tell without seeing your code that derives it. My guess is that you're using `DBI` (using the `odbc` *driver*) to get to the DBMS. I don't know of any function in `DBI` that returns actual data as a *vector*, all data are returned as a `data.frame` (even if 0 rows, etc); the only time a non-frame is returned is with errors, inserts/deletions (number of rows inserted/deleted), and updates (similar to inserts). Oh, and listing tables or fields ... which is not what it seems you are doing. – r2evans Sep 25 '20 at 20:13
  • Edited the question – Tim Wilcox Sep 25 '20 at 20:15
  • You're still using `%/%`, which I'm now fairly confident is wrong. Replace it with `%>%` (assuming you have already done `library(dplyr)`). – r2evans Sep 25 '20 at 20:16
  • Also, on cursory exploration, I'm guessing that you have `periodtype` defined both as a column within `labforce1` *and* as a regular variable in the environment; if the latter were not true, you would instead be getting an error about `object 'periodtype' not found`. – r2evans Sep 25 '20 at 20:19
  • Please include `dput(head(labforce1))` in your question. (I can think of no way that the return value from that `dbGetQuery` would be an object of class `logical`.) – r2evans Sep 25 '20 at 20:20
  • You are correct, @R2Evans, changed it %>% to same result. – Tim Wilcox Sep 25 '20 at 20:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222090/discussion-between-r2evans-and-tim-wilcox). – r2evans Sep 25 '20 at 20:21

1 Answers1

3

From the chat, the issue is that the OP was copying text from somebody else's paper. That code was including R's "line continuation +" indicator, as in

> labforce1 %>%
+   filter(periodtype=='03')

Because many questions on SO include that because the asker copied it from their R console, it felt safe to assume that that was an attribute of the console, not the actual code being typed in. However, in this case, it was literally typed (pasted) in and effectively

labforce1 %>% + filter(periodtype=='03')

which is more-obviously not valid R syntax for what we're trying to do.

Bottom-line: remove the + (and any other line-continuation indicators like that) when pasting code from a paper, script, or similar.

labforce1 %>%
  filter(periodtype == '03')

(This is one reason why many reprex tools differentiate code and output with clear techniques such as removing line-continuation marks and prepending all output with #.)

r2evans
  • 141,215
  • 6
  • 77
  • 149