Reputation: 1331
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")
>
Upvotes: 0
Views: 258
Reputation: 160417
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 #
.)
Upvotes: 3