logo
Tags down

shadow

case_when() not working: Error in mutate_impl(.data, dots)


By : user2176505
Date : October 14 2020, 02:21 PM
it helps some times Have you tried restarting your R session? Because for me it seems to work using the default dplyr notation as suggested by phiver.
code :
library(tidyverse)

mydata<-structure(list(condition = c("A", "B", "C", "D", "E", "A", "B", "C", "D", "E"), r_condition = c(-0.753941712419928, 0.18796391661835, 1.07774993391177, 0.141452804751733, -1.67697957194689, -0.730021740287756, 0.30088191760836, 0.820982130392851, 0.20992883143208, -1.75797911497123), den_a = c(-1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457, -1106.36423037457), den_b = c(1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413, 1013.85560135413), den_c = c(4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743, 4340.91752722743), den_d = c(1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632, 1078.95631612632), den_e = c(-6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366, -6530.58467431366)), row.names = c(NA, -10L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "condition", drop = TRUE, indices = list(c(0L, 5L), c(1L, 6L), c(2L, 7L), c(3L, 8L), c(4L, 9L)), group_sizes = c(2L, 2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(condition = c("A", "B", "C", "D", "E")), row.names = c(NA, -5L), class = "data.frame", vars = "condition", drop = TRUE))

x <- mydata%>% mutate(den = case_when (
  condition=="D" ~ den_d,
  condition=="C" ~ den_c, 
  condition=="E" ~ den_e, 
  condition=="B" ~ den_b,
  condition=="A" ~ den_a))

x
#> # A tibble: 10 x 8
#> # Groups:   condition [5]
#>    condition r_condition  den_a den_b den_c den_d  den_e    den
#>    <chr>           <dbl>  <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>
#>  1 A              -0.754 -1106. 1014. 4341. 1079. -6531. -1106.
#>  2 B               0.188 -1106. 1014. 4341. 1079. -6531.  1014.
#>  3 C               1.08  -1106. 1014. 4341. 1079. -6531.  4341.
#>  4 D               0.141 -1106. 1014. 4341. 1079. -6531.  1079.
#>  5 E              -1.68  -1106. 1014. 4341. 1079. -6531. -6531.
#>  6 A              -0.730 -1106. 1014. 4341. 1079. -6531. -1106.
#>  7 B               0.301 -1106. 1014. 4341. 1079. -6531.  1014.
#>  8 C               0.821 -1106. 1014. 4341. 1079. -6531.  4341.
#>  9 D               0.210 -1106. 1014. 4341. 1079. -6531.  1079.
#> 10 E              -1.76  -1106. 1014. 4341. 1079. -6531. -6531.


Share : facebook icon twitter icon

Error in mutate_impl(.data, dots) : binding not found


By : Nicolas Cloutier
Date : March 29 2020, 07:55 AM
To fix the issue you can do I would like to combine several of my files into one file. I found the codes from one of the post in the website and adapted it to my own use as followed: , I've run your code and got the same error message
code :
read_csv("test_.1.csv")
Parsed with column specification:
cols(
  `Iso   rep set Condit  hpi OD` = col_character()
)
# A tibble: 6 × 1
  `Iso   rep set Condit  hpi OD`
                           <chr>
1  A22.1   1   3   T27 84  0.232
2   A22.1   2   3   T27 84  0.23
3  A22.1   3   3   T27 84  0.214
4  D2.1    1   3   T27 84  0.049
5  D2.1    2   3   T27 84  0.054
6  D2.1    3   3   T27 84  0.049
file_names <- list.files(pattern = "test_.*\\.csv", full.names = TRUE)
library(data.table)
df2 <- 
  rbindlist(
    lapply(file_names, fread), idcol = "file_id"
  )[, file_id := basename(file_names)[file_id] # add origin
    ][, setnames(.SD, "Iso", "Isolate")] # rename one column

df2
#        file_id Isolate rep set Condit hpi    OD
# 1: test_.1.csv   A22.1   1   3    T27  84 0.232
# 2: test_.1.csv   A22.1   2   3    T27  84 0.230
# 3: test_.1.csv   A22.1   3   3    T27  84 0.214
# 4: test_.1.csv    D2.1   1   3    T27  84 0.049
# 5: test_.1.csv    D2.1   2   3    T27  84 0.054
# 6: test_.1.csv    D2.1   3   3    T27  84 0.049
# 7: test_.2.csv   A22.1   1   3    T27  72 0.191
# 8: test_.2.csv   A22.1   2   3    T27  72 0.186
# 9: test_.2.csv   A22.1   3   3    T27  72 0.179
#10: test_.2.csv    D2.1   1   3    T27  72 0.048
#11: test_.2.csv    D2.1   2   3    T27  72 0.053

dplyr : Error in mutate_impl(.data, dots) : argument 'times' incorrect


By : mwebster
Date : March 29 2020, 07:55 AM
I hope this helps you . The error comes from rep, the times parameter needs to be either 1 or the same length as x, which is not the case here:
code :
rep("H", c(1,2))
Error in rep("H", c(1, 2)) : invalid 'times' argument
r %>% mutate(y = strrep("H",nchar(read_seq)))

# A tibble: 2 x 3
#  read_seq unique_id        y
#     <chr>     <dbl>    <chr>
#1 AATTGGCC         1 HHHHHHHH
#2   GGGTTT         2   HHHHHH
r %>% mutate(y = stri_rand_strings(length(read_seq), nchar(read_seq)))

# A tibble: 2 x 3
#  read_seq unique_id        y
#     <chr>     <dbl>    <chr>
#1 AATTGGCC         1 H0flAbNS
#2   GGGTTT         2   QrQOLj

r: Error in mutate_impl(.data, dots) : Evaluation error: object '72L' not found


By : Kevin
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further The main problem here is that when you attach() your data frame first you mask a number of objects that you need. Generally, don't use attach. If you want to work with an attached data set, use it inside a call to with, and certainly don't combine this with dplyr or other tidyverse functions.
With a clean environment (no data or namespaces attached), use dplyr methods throughout, like this. Other than not attaching, notice the two other changes to your code. (1) use mutate to set the Date column within the dplyr pipe. (2) x = Hours_Time, rather than Time. If you've previously run attach(new_data), do detach(new_data) first, UNTIL you have a clean environment. If you've run it multiple times, you're going to have to detach multiple times.
code :
library(dplyr)
library(sugrrants)
new_data %>%
  mutate(Date = as.Date(Date, origin = "1/1/2000")) %>%
  frame_calendar(x = Hours_Time, y = Hourly_Counts, date = Date)
#       Date_Time    Time Year   Month Mdate      Day Hours_Time Hourly_Counts       Date
# 1 1/1/2015 0:00 0:00:00 2015 January     1 Thursday          1            72 2015-01-01
# 2 1/1/2015 1:00 1:00:00 2015 January     1 Thursday          2            48 2015-01-01
# 3 1/1/2015 2:00 2:00:00 2015 January     1 Thursday          3            53 2015-01-01
# 4 1/1/2015 3:00 3:00:00 2015 January     1 Thursday          4            84 2015-01-01
# 5 1/1/2015 4:00 4:00:00 2015 January     1 Thursday          5            68 2015-01-01
#   .Hours_Time .Hourly_Counts
# 1    1.454167      0.6833333
# 2    1.691667      0.0500000
# 3    1.929167      0.1819444
# 4    2.166667      1.0000000
# 5    2.404167      0.5777778
new_data <- read.table(text = "Date_Time   Time    Date    Year    Month   Mdate   Day Hours_Time  Hourly_Counts
'1/1/2015 0:00'   0:00:00 2015-01-01  2015    January 1   Thursday    1   72
                       '1/1/2015 1:00'   1:00:00 2015-01-01  2015    January 1   Thursday    2   48
                       '1/1/2015 2:00'   2:00:00 2015-01-01  2015    January 1   Thursday    3   53
                       '1/1/2015 3:00'   3:00:00 2015-01-01  2015    January 1   Thursday    4   84
                       '1/1/2015 4:00'   4:00:00 2015-01-01  2015    January 1   Thursday    5   68", header = TRUE, stringsAsFactors = FALSE)

Error in mutate_impl(.data, dots) : Evaluation error: Argument 2 must be type character


By : john
Date : March 29 2020, 07:55 AM
To fix this issue "Argument 2" apparently just refers to the vectors being made anew in one data frame and merged. I started thinking that might be the case so I found the vectors that were integer and character in their respective dataframes by brute force just by going down the list of ~30 vectors being coalesced and commenting them out find which ones made the code work again when they were "off". So, this fixed it: change "yr" to integer in the offending data frame:
code :
df1$yr<-as.integer(as.character(df1$yr))

mutate_impl(.data, dots) Evaluation error: object not found


By : user1909445
Date : March 29 2020, 07:55 AM
Any of those help I had some working code. I had to update R (and install all packages again) and when I try to run the code again hit a wall. Here's a toy example: , Try adding funs()
code :
library(dplyr)

mtcars %>% 
  mutate_if(is.numeric, funs(./cyl))
#>         mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> 1  3.500000   1 160.0 110 3.90 2.620 16.46  0  1    4    4
#> 2  3.500000   1 160.0 110 3.90 2.875 17.02  0  1    4    4
#> 3  5.700000   1 108.0  93 3.85 2.320 18.61  1  1    4    1
#> 4  3.566667   1 258.0 110 3.08 3.215 19.44  1  0    3    1
#> 5  2.337500   1 360.0 175 3.15 3.440 17.02  0  0    3    2
#> 6  3.016667   1 225.0 105 2.76 3.460 20.22  1  0    3    1
#> 7  1.787500   1 360.0 245 3.21 3.570 15.84  0  0    3    4
#> 8  6.100000   1 146.7  62 3.69 3.190 20.00  1  0    4    2
#> 9  5.700000   1 140.8  95 3.92 3.150 22.90  1  0    4    2
#> 10 3.200000   1 167.6 123 3.92 3.440 18.30  1  0    4    4


mtcars %>% 
  mutate_if(is.numeric, funs(div = ./cyl))
#>     mpg cyl  disp  hp drat    wt  qsec vs am gear carb  mpg_div cyl_div
#> 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4 3.500000       1
#> 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4 3.500000       1
#> 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1 5.700000       1
#> 4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1 3.566667       1
#> 5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2 2.337500       1
#> 6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1 3.016667       1
#> 7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4 1.787500       1
#> 8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2 6.100000       1
#> 9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2 5.700000       1
#> 10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4 3.200000       1
#>    disp_div   hp_div  drat_div    wt_div qsec_div    vs_div    am_div
#> 1  26.66667 18.33333 0.6500000 0.4366667 2.743333 0.0000000 0.1666667
#> 2  26.66667 18.33333 0.6500000 0.4791667 2.836667 0.0000000 0.1666667
#> 3  27.00000 23.25000 0.9625000 0.5800000 4.652500 0.2500000 0.2500000
#> 4  43.00000 18.33333 0.5133333 0.5358333 3.240000 0.1666667 0.0000000
#> 5  45.00000 21.87500 0.3937500 0.4300000 2.127500 0.0000000 0.0000000
#> 6  37.50000 17.50000 0.4600000 0.5766667 3.370000 0.1666667 0.0000000
#> 7  45.00000 30.62500 0.4012500 0.4462500 1.980000 0.0000000 0.0000000
#> 8  36.67500 15.50000 0.9225000 0.7975000 5.000000 0.2500000 0.0000000
#> 9  35.20000 23.75000 0.9800000 0.7875000 5.725000 0.2500000 0.0000000
#> 10 27.93333 20.50000 0.6533333 0.5733333 3.050000 0.1666667 0.0000000
Related Posts Related Posts :
  • Multinomial probit regression with mixed type explanatory variables
  • How can I make a variable in a dataset containing a vector of all numbers between two other variables?
  • How to extract the trailing digits from a string in R?
  • Select values based on other columns
  • readLines killing R in purrr::map
  • Subset rows based on "start and stop" strings
  • How to add a column to lists within a list without losing their names?
  • Plotting the means in ggplot, without using stat_summary()
  • R :Looping through each 5 rows of data frame and imputing incremental value
  • In R, is growing a list just as inefficient as growing a vector?
  • Flexdashboard, rhandsontable: how to programmatically access user updated table?
  • Creating Summary Table from R Variables
  • Average over groups and include previous groups
  • R: data.table count rows on specific columns > 0
  • Transform (shuffle) just 2 Fields in a Dataframe
  • Issue with replacing string by match in R
  • (very) Simple quantstrat trading model using logistic regression
  • R - count maximum number of consecutive dates
  • Problems using tidyr separate on "|"
  • Default value when calling a function in a for loop
  • Finding values in a matrix from list of values in R
  • count 0's in a zoo (or dataframe) object
  • Finding the first non-zero year in data frame for multiple variables using tidyverse
  • ggplot2 - how to assign geom_text with arrow icon to second yaxis scale
  • regex fails with dollar sign
  • Drop first element of list of lists, condense list of lists? Too many elements?
  • R - how to apply output of ifelse(str_detect ...) to whole group
  • caret package confusion matrix define positive case with multiple classes
  • Generating a pairwise 'distance' matrix
  • Change all R columns names using a reference file
  • In R & dabestr, how do I get grouped differences correctly?
  • Exclude or set a unique color to the bottom triangle of a correlation matrix heatmap
  • r shiny observe function clears text input
  • Split column by multiple delimiters, keeping delimiters
  • How to random search in a specified grid in caret package?
  • merge 2 data frames in a loop for each column in one of them
  • how to edit the codes for the summary of R S4 Object?
  • Remove specific rows in R
  • Flatten JSON list into data frame
  • Filtering a dataset and making a ggplot
  • Align cells vertically to be at the bottom flextable
  • R speed up sapply
  • invalid subscript type 'list' Azure Machine Learning
  • Use rollapply with xts object and an anonymous defined function
  • Isolate data frames from a spreadsheet to create a list
  • Error in xts, as.POSIXct "'order.by' cannot contain 'NA', 'NaN', or 'Inf'"
  • Column splitting in R
  • number similar/duplicated rows in R
  • Count the number of times each value appears in a row dataframe r
  • how to vectorise my code in r using for loop?
  • A function to fill in a column with NA of the same type
  • Network flow balancing constraint in R
  • Adding main titles from list to graphs in for loop
  • create a matrix in Perl or R if data is provided in CSV file
  • Passing column names as string to with
  • R - filtering rows and summing
  • How to change the order of fill aesthetic in faceted ggplot?
  • Function to remove outliers by group from dataframe
  • How to find the difference of max & min values in one group in a variable in a dataframe
  • Convert unicode to a readable string
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org