Search
 
SCRIPT & CODE EXAMPLE
 

R

conditional mean statement r

dataset <- data.frame(continuous = rnorm(n = 10),
                      binary = sample(x = 0:1, size = 10, replace = TRUE))

dataset
#>     continuous binary
#> 1  -0.01978487      0
#> 2  -1.14185292      0
#> 3   0.20931787      0
#> 4  -0.63720730      0
#> 5   1.07750407      1
#> 6  -1.59274225      0
#> 7  -0.48722740      1
#> 8  -0.64151044      0
#> 9  -0.64111755      0
#> 10  0.99598287      1

# your method
mean(dataset[dataset$binary == 1, 1])
#> [1] 0.5287532
mean(dataset[dataset$binary == 0, 1])
#> [1] -0.6378425

# using by
by(data = dataset$continuous, INDICES = dataset$binary, FUN = mean)
#> dataset$binary: 0
#> [1] -0.6378425
#> -------------------------------------------------------- 
#> dataset$binary: 1
#> [1] 0.5287532
Comment

PREVIOUS NEXT
Code Example
R :: r - if value in a df is between two number then add 1 
R :: r install package 
Rust :: rust allow unused 
Rust :: rust dictionary 
Rust :: rust string to char array 
Rust :: rust create bigint from string 
Rust :: bevy assets image as sprite 
Rust :: rust request get headers 
Rust :: linking with `link.exe` failed: exit code: 1189 
Rust :: closure rust 
Rust :: rust string slice 
Rust :: find last element of vec rust 
Rust :: rust iterate vector backwards 
Rust :: armanriazi•rust•smartpointer•box•cons 
Rust :: what is () in rust 
Rust :: armanriazi•rust•thread•strateges 
Rust :: armanriazi•rust•error•E0277•the size for values of type `str` cannot be known at compilation time 
Rust :: armanriazi•rust•error•E0308•mismatched types 
Rust :: rust program name 
Rust :: armanriazi•rust•smartpointer•deref•coercion 
Rust :: armanriazi•rust•error•E0277•the trait bound `` is not satisfied 
Rust :: rust empty vector 
Lua :: how to get a random number in lua 
Lua :: roblox how to make debounce 
Lua :: roblox how to find something in table 
Lua :: string to int lua 
Lua :: lua trim 
Lua :: lua oop 
Lua :: How to Register a command in Lua 
Lua :: fivem get closest player 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =