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?
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.
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.
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")
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 .
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.
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).
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.
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.
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)))
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
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With