logo
down
shadow

Can dplyr::case_when return mix of NAs and non-NAs?


Can dplyr::case_when return mix of NAs and non-NAs?

Content Index :

Can dplyr::case_when return mix of NAs and non-NAs?
Tag : r , By : jaime
Date : November 23 2020, 03:01 PM

will help you Here is the problem with class. We need NA_real to match the numeric type
mtcars %>% 
      mutate(thing = case_when(cyl >= 6 ~ 1,
                               cyl < 6 ~ NA_real_ )) 
mtcars[1:3,] %>% 
       mutate(thing = case_when(cyl == 6 ~ 1, 
                                cyl < 6 ~ NA_real_, 
                                cyl > 6 ~ NA_real_ ))  %>%
       select(cyl, thing)
# cyl thing
#   6     1
#   6     1
#   4    NA

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Use NSE in dplyr::case_when


Tag : r , By : Steve
Date : March 29 2020, 07:55 AM
should help you out You need parenthesis around !!vars, so !!var1 == "a" ~ 1 should be (!!var1) == "a" ~ 1:
fun_test <- function(df, var1, var2) {

    var1 <- enquo(var1)
    var2 <- enquo(var2)

    df <- mutate(df,
                 c = case_when(
                     (!!var1) == "a" ~ 1,
                     (!!var1) == "b" ~ 2,
                     (!!var1) == "c" ~ 3
                 ),
                 d = case_when(
                     (!!var2) > 0 ~ 1,
                     (!!var2) < 0 ~ 0
                 ))

    df       
}

fun_test(data_test, a, b)

#    a           b c d
# 1  a  0.70352390 1 1
# 2  a -0.10567133 1 0
# 3  a -1.25864863 1 0
# 4  a  1.68443571 1 1
# 5  a  0.91139129 1 1
# 6  b  0.23743027 2 1
# 7  b  1.21810861 2 1
# 8  b -1.33877429 2 0
# 9  b  0.66082030 2 1
# 10 b -0.52291238 2 0
# 11 c  0.68374552 3 1
# 12 c -0.06082195 3 0
# 13 c  0.63296071 3 1
# 14 c  1.33551762 3 1
# 15 c  0.00729009 3 1

use dplyr case_when programmatically


Tag : r , By : Andrew Bailey
Date : March 29 2020, 07:55 AM
may help you . I'm trying to use dplyr case_when and pass variable but the result was not as expected. , This is an order of operations problem, you need to use brackets:
quo_function <- function(df, var) {
  df %>%
    mutate(cg = case_when((!!var) <= 2 ~ "low",
                          (!!var) > 2 ~ "high"))  
}

Evaluation in dplyr::case_when()


Tag : r , By : John Miller
Date : March 29 2020, 07:55 AM
Any of those help
case_when() evaluates all statements one by one regardless if a previous one is true or not (since it does evaluate TRUE ~ as.character(x) which is the last one)

dplyr behaviour within case_when and lag


Tag : r , By : n3txpert
Date : March 29 2020, 07:55 AM
Hope this helps You are looking for something like the last observation carried forward e.g zoo::na.locf or tidyr::fill, but I'll use something simple like:
library(dplyr)
testdat %>% 
   mutate(date = lubridate::ymd(datestring)) %>% group_by(studyid) %>% 
   arrange(studyid, date) %>% mutate(prevalent=cumsum(lag(incident,default = 0)==1))

# A tibble: 8 x 5
# Groups:   studyid [2]
  studyid datestring incident date       prevalent
  <chr>   <chr>         <dbl> <date>         <int>
1 1       2000-01-01        0 2000-01-01         0
2 1       2001-01-01        1 2001-01-01         0
3 1       2002-01-01        0 2002-01-01         1
4 1       2003-01-01        0 2003-01-01         1
5 2       2003-01-01        0 2003-01-01         0
6 2       2004-01-01        1 2004-01-01         0
7 2       2005-01-01        0 2005-01-01         1
8 2       2006-01-01        0 2006-01-01         1

Using complex RHS expressions in `dplyr::case_when()` in the context of `dplyr::mutate()`


Tag : r , By : fstender
Date : March 29 2020, 07:55 AM
To fix this issue I think the issue may have less to do with NSE than the fact that pluck is not vectorized - as currently written pluck is not evaluated once per row, but rather all rows are attempted to be run through pluck at once. However, as you figured out, pluck needs a single number input, not a vector.
One way to solve this is to map the function across the rows, using your code as a little lambda-style function. Note that you need to use map_dbl to coerce a numeric value, otherwise map would return a list and everything would explode :-)
x %>% 
  mutate(`New Number` = case_when(
    fruit == "apple" ~ map_dbl(`Old Letter`, ~ pluck(key_list, ., "foo")) * 10,
    fruit == "orange" ~ map_dbl(`Old Letter`, ~ pluck(key_list, ., "foo")) * 100,
    fruit == "grape" ~ map_dbl(`Old Letter`, ~ pluck(key_list, ., "foo")) * 1000
  ))

# # A tibble: 6 x 4
#   fruit  `Old Letter` `Old Number` `New Number`
#   <chr>  <chr>               <dbl>        <dbl>
# 1 apple  a                       9           10
# 2 orange a                       8          100
# 3 grape  b                       7         3000
# 4 apple  c                       6           50
# 5 apple  c                       5           50
# 6 orange c                       4          500
Related Posts Related QUESTIONS :
  • Best method to extract the first instance of a string between specified keywords using data.table
  • ignore optional combination of alphanumeric characters in str_extract
  • Why tracemem shows two copies when modification occurs inside function body?
  • Can't use mppm on multitype point patterns
  • How to move selected matrix rows to top of matrix based on a selection vector of row names
  • Combining expressions with a common operator
  • Passing string through multiple filters for matching
  • Convert two columns in R to rows of unique occurrence
  • How to create a dataframe using a function based on user-input?
  • How to access the visited vertices in a given shortest path using R igraph
  • Differences in Unicode character output with print()
  • Extracting Function or Objects from a String and then Piping Them with Magrittr/Dplyr
  • renderUI not evaluated until it is rendered
  • Find the maximum absolute value by row in an R data frame
  • Extracting data from irregular lists using purrr:map()
  • transforming data based on range of column in r
  • Identify and subset rows with some similar information
  • converting character from mongolite to timestamp in R
  • Create list from two vectors with every combo of each
  • Error in running a spread because of unique 'key combinations'; combining rows of data
  • visualize numerical strings as a matrixed heatmap
  • how to make a blocked matrix?
  • How to summarize with two functions using with dplyr
  • Dataframe is no longer the same after being saved to Excel and read back in
  • Create duplicate rows using based on availability of data
  • Keep empty groups when grouping with data.table in R
  • Grouping of Event Time Data based on multiple, iterative conditions
  • Formatting Numbers in Flextable for Specific Columns
  • How to store results from for-loop into a dataframe
  • How to select the values in my dataframe which has logical operator "<" (less than), divide them by two, an
  • Rowwise extract data between two strings
  • Convert a string separate by . and +
  • stacking function for values in R
  • dplyr coerces characters to factors
  • How do I use spread and group_by on a single row dataset
  • Replacing values in one matrix with values from another
  • Aggregate data and exclude duplicates in one column
  • Perform an R data.table binary search with OR select
  • How can I include a function in the Standard Deviation parameter of pnorm
  • How to get a tidy excel output of P values from R
  • Rotate boxplot legend (R, ggplot2)
  • dplyr::n() returns “Error: Error: n() should only be called in a data context ”
  • Extract fix columns and one variable column from a list of df´s in R
  • A function that can translate DNA sequence to binary code
  • I want to extract 365 netcdf files using loop
  • rvest vs RSelenium results for text extracting
  • Converting wide data to tall data
  • How to remove vertical white lines when using ggsave in R?
  • R-Shiny error: "renderDataTable" and "server=FALSE"
  • Read csv file with selected rows using data.table's fread
  • how to resolve an error like non numeric argument to binary argument?
  • If value exists in environment
  • R get one value according to some rules in each group
  • Use any apply method to find difference between max and min score for each students
  • subsetting a dataframe by existing object
  • Parsing time formats in R
  • How to train a multiple linear regression model to find the best combination of variables?
  • R replace value in a column by a dictionary table without using merge or join
  • write list of dataframes to multiple excel files
  • How do I prevent redundant updating in R Shiny?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com