Search
 
SCRIPT & CODE EXAMPLE
 

R

dplyr mutate with conditional values

myfile %>% mutate(V5 = (V1 == 1 & V2 != 4) + 2 * (V2 == 4 & V3 != 1))
or
myfile %>% mutate(V5 = ifelse(V1 == 1 & V2 != 4, 1, ifelse(V2 == 4 & V3 != 1, 2, 0)))

  V1 V2 V3 V4
1  1  2  3  5
2  2  4  4  1
3  1  4  1  1
4  4  5  1  3
5  5  5  5  4
...


  V1 V2 V3 V4 V5
1  1  2  3  5  1
2  2  4  4  1  2
3  1  4  1  1  0
4  4  5  1  3  0
5  5  5  5  4  0
Comment

PREVIOUS NEXT
Code Example
R :: not equals r 
R :: how to set a dataframe as a value in a list in r 
R :: extract df from lm in r 
R :: list variables r 
R :: get plot title over two lines R 
R :: How to use group_by inside a function? 
R :: R ggplot 2 legend text 
R :: calculating every column means by dplyr 
R :: R grid all possibilites between two vectors 
R :: change color theme of fill R 
R :: meaning of %% R 
R :: how to remove null values in r 
Rust :: random number generator in rust 
Rust :: print number as binary in rust 
Rust :: rust nesting loops 
Rust :: rust reverse an array 
Rust :: rust struct default values 
Rust :: enum rust 
Rust :: concat string rust 
Rust :: string and str to string rust 
Rust :: armanriazi•rust•thread•recv•try_recv 
Rust :: armanriazi•rust•error•[E0369]: binary operation `=` cannot be applied to type `T` 
Rust :: Counting bits Kernighan style 
Rust :: armanriazi•rust•trait•external•implement•coherence•orphan 
Rust :: armanriazi•rust•concept•jargon 
Lua :: how to execute scripts when a button gui is pressed roblox 
Lua :: tostring lua 
Lua :: lua globals 
Lua :: for i = 1 to n roblox 
Lua :: lua trim 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =