owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
18 December, 2018
Welcome!
## Challenge 2 - list errors
List your errors in the box underneath while solving the challenge 2, each time by `*` + `your_error_message`. If an error already is enlisted, put a * in front of the existing error
*Warning: 44808 parsing failures.
```
** Error in mutate_impl(.data, dots) :
Evaluation error: could not find function "year".
* Error in FUN(X[[i]], ...) : object 'month_sum' not found
* Error in filter_impl(.data, quo) :
Evaluation error: is_string(match) is not TRUE.
* Error in filter_impl(.data, quo) :
Evaluation error: character string is not in a standard unambiguous format.
* Error: `datetime` (`datetime = 2012`) must not be named, do you need `==`?
Call `rlang::last_error()` to see a backtrace
* Error: stat_count() must not be used with a y aesthetic.
* Error in summarise_impl(.data, dots) :
Column `value` must be length 1 (a summary value), not 744
* Error in barplot.default(klemskerke_sum) :
'height' must be a vector or a matrix
* Error: unexpected '<' in:
" filter(jaar == 2012, quality_code == 220) %>%
mutate(ifelse(value =<"
* Error in filter_impl(.data, quo) :
Evaluation error: character string is not in a standard unambiguous format.
* Error in year(Rainfall$datetime) : could not find function "year"
* Error in as.Date(datetime) : object 'datetime' not found
* Error: `jaar` (`jaar = 2012`) must not be named, do you need `==`?
* Unmatched open bracket
* Error: stat_count() must not be used with a y aesthetic.
* Error: stat_bin() must not be used with a y aesthetic.
* Error in geom_hist() : could not find function "geom_hist"
* Error: stat_count() must not be used with a y aesthetic.
* Error in mutate_impl(.data, dots) :
Evaluation error: invalid 'trim' argument.
* Error in mutate_impl(.data, dots) :
* Evaluation error: comparison (1) is possible only for atomic and list types.
* Error in pmin(y, 0) : object 'y' not found
```
```
klem2012 %>%
+ mutate(month = lubridate::month(datetime)) %>%
+ group_by(month) %>%
+ summarise(rainfall = sum(value, na.rm = TRUE)) %>%
+ ggplot(aes(x = month, y = rainfall)) %>%
+ geom_col()
Error: `mapping` must be created by `aes()`
Did you use %>% instead of +?
```
```
data <- read_csv2("./20180123_rainfall_klemskerke_clean.csv")
Using ',' as decimal and '.' as grouping mark. Use read_delim() for more control.
Parsed with column specification:
cols(
`datetime,value,quality_code` = col_character()
)
```
```
> data %>% mutate(jaar = year(datetime)) -> data
Error in mutate_impl(.data, dots) :
Evaluation error: could not find function "year".
```
```
> data2012Q220$value[data2012Q220$value < 0,01] <- 0
Error in data2012Q220$value[data2012Q220$value < 0, 1] <- 0 :
incorrect number of subscripts on matrix
```
### your subtitle - example
As an **example**:
```r
library(tidyverse)
...
```
### your subtitle
```r
...
```
### Code worked perfect last week and now this eror
Happens in a loop when combining select and rbind
```r
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘select’ for signature ‘"data.frame"’
```
## regex in stringr
While passing regex patterns to fucntion `str_detect()` of package `stringr` I got this error:
```r
library(stringr)
str_detect("GP578", pattern = regex("GP\d+"))
> Error: '\d' is an unrecognized escape in character string starting ""GP\d"
```
However, the regex expression is correct and tested online: https://regex101.com/
### Solution
```r
str_detect("GP578", pattern = regex("(GP\\d+)"))
```
## Bug in tidyr 0.8.2
I got this error after installing version 0.8.2 of tidyr but not in earlier versions (e.g. 0.8.1):
Error: Column `Rijnr` not found in `.data`
Call `rlang::last_error()` to see a backtrace
```r
# Code is meant to return the number of rows of the dataset,
# other results are printed to illustrate the problem
library(tibble)
library(dplyr)
library(tidyr)
mijnFunctie <- function(Dataset) {
mijnBerekening <- function(Data) {
Data %>%
transmute(
.data$Rijnr,
Rijplus = .data$Rijnr + 1
)
}
Resultaat <- Dataset %>%
group_by(.data$Variabele) %>%
do(Waarde = mijnBerekening(.)) %>%
unnest(.data$Waarde)
print("Resultaat na berekening:")
print(Resultaat)
Resultaat <- Resultaat %>% select(.data$Rijnr, .data$Rijplus)
print("Resultaat na selectie:")
print(Resultaat)
print(paste("Aantal rijen:", nrow(Resultaat)))
}
#that's how the function should work
Dataset1 <- tibble(Variabele = "x", Rijnr = 1)
mijnFunctie(Dataset1)
#unexpected behaviour if the dataset is empty
Dataset2 <- Dataset1 %>%
filter(.data$Rijnr != 1)
mijnFunctie(Dataset2)
```
## error while making a data.frame
```r
# Code
library(assertthat)
df <- data.frame(col1 = c(1,2,3),
col2 = c(11,22,33),
col3 = c("test1", "test2", "test3"),
col4 <- c("a", "b", "c"),
stringsAsFactors = FALSE)
name_cols <- c("col1", "col2", "col3", "col4")
assert_that(all(name_cols %in% names(df)))
# Output
> Error: Elements 4 of name_cols %in% names(df) are not true
```
### Solution
Whiel defining the df, it should be `col4 = c("a", "b", "c")`, not `col4 <- c("a", "b", "c")`.
## error while trying to find number of NAs in a column
I want to know the number of rows with NA and not NA for a specific column. This is my code and error message:
```r
df <- data.frame(col1 = c("a", NA, NA, NA, "c"))
> df %>%
summarize(n = is.na(col1))
Error in summarise_impl(.data, dots) : Column `n` must be length 1 (a summary value), not 5
```
### Solution
```r
df <- data.frame(col1 = c("a", NA, NA, NA, "c"))
df %>%
group_by(is.na(col1)) %>%
summarize(count = n()) %>%
rename("is_na" = "is.na(col1)")
# A tibble: 2 x 2
is_na count
<lgl> <int>
1 FALSE 2
2 TRUE 3
```
## error while joining dataframes
error got while using all kind of `*_join()` function of dplyr package:
```r
a <- a %>%
left_join(b, by = col_to_join)
> Error in common_by(by, x, y) : object ' col_to_join' not found
```
### Solution
```r
a <- a %>%
left_join(b, by = "col_to_join")
```
## repeat elements of a vector twice
I just want to make 2 copies of a vector:
```
> a * 2
Error in a * 2 : non-numeric argument to binary operator
```