Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ifelse inside map function in R

I am having problems with this ifelse sentence inside map function:

df<-list(mtcars,mtcars)

All I want to do is to organize each dataframe of this list this way:slice(x,c(n(),2:(n()-1),1))

map(df, ~   slice(.x,c(n(),2:(n()-1),1)))   # it works well. 

But I cant do this inside ifelse when I set the condition x$cyl == 4:

map(df, ~   ifelse(sum(.x$cyl == 4) > 0, slice(.x,c(n(),2:(n()-1),1)), .x)) # The output is weird

I tried to do the same using lapply but its not working:

lapply(df, function(x) ifelse(sum(x$cyl == 4) > 0, slice(x,c(n(),2:(n()-1),1)),x))

Any help?

like image 827
Laura Avatar asked Apr 28 '21 23:04

Laura


People also ask

How do you start writing an if statement in R?

Syntax. if(boolean_expression) { // statement(s) will execute if the boolean expression is true. } else { // statement(s) will execute if the boolean expression is false. } If the Boolean expression evaluates to be true, then the if block of code will be executed, otherwise else block of code will be executed.

How do I create a nested Ifelse in R?

Nested if...else Statements in RIf x is greater than 0, the code inside the outer if block is executed. Otherwise, the code inside the outer else block is executed. The inner if...else block checks whether x is even or odd. If x is perfectly divisible by 2, the code inside the inner if block is executed.

What is the syntax of Ifelse () function?

Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it's false. For example: =IF(A2>B2,"Over Budget","OK")

How does Ifelse function work in R?

R ifelse() Function Most of the functions in R take a vector as input and return a vectorized output. Similarly, the vector equivalent of the traditional if...else block is the ifelse() function. The output vector has the element x if the output of the test_expression is TRUE .

What is ifelse() function in R?

In this article, you’ll learn about ifelse () function. This is a shorthand function to the traditional if…else statement. Vectors form the basic building block of R programming. Most of the functions in R take vector as input and output a resultant vector.

Is there a vector equivalent of ifelse in R?

Similar to this concept, there is a vector equivalent form of the if…else statement in R, the ifelse () function. Syntax of ifelse () function ifelse (test_expression, x, y) Here, test_expression must be a logical vector (or an object that can be coerced to logical).

How to use the ifelse function instead of the if and else?

This Example shows how to use the ifelse function instead of the if and else statements shown in Example 1. Within the ifelse function, we have to specify three parameters: The logical condition we want to test. What should happen in case the logical condition is TRUE. What should happen in case the logical condition is FALSE.

How do I use the map() function in R?

The map () function from the purrr package in R can be used to apply some function to each element in a vector or list and return a list as a result. The following examples show how to use this function in different scenarios.


Video Answer


2 Answers

We can use map_if to apply the function only when a condition is TRUE.

library(tidyverse)

df <- list(mtcars,mtcars)

df2 <- map_if(df, .p = function(y) any(y$cyl == 4),
                  .f = ~slice(.x, c(n(), 2:(n()-1), 1)))
like image 147
www Avatar answered Nov 12 '22 21:11

www


This approach checks each data.frame in the list for any rows where cyl == 4. If it meets that condition, your desired slice is returned. Otherwise, the data.frame is returned unchanged.

library(dplyr)


df<-list(mtcars,mtcars)



lapply(df, function(x) {
  if (any(x$cyl == 4)) {
    slice(x, c(n(), 2:(n()-1), 1)) 
    } else { 
      x 
    }
  })

If you strongly prefer map:

library(dplyr)
library(purrr)

df<-list(mtcars,mtcars)



map(df, ~ 
  if (any(.x$cyl == 4)) {
    slice(.x, c(n(), 2:(n()-1), 1)) 
    } else { 
      .x 
    }
  )
like image 29
Alec B Avatar answered Nov 12 '22 20:11

Alec B