AS93
August 8, 2019, 1:01pm
1
Hello,
I'm trying to create an indicator function 1(•) for my variable "demand", but I'm not sure if this is correct. I have tried it like this:
#for the indicator function 1(demand=0)
summary(as.numeric(demand==0))
#for the indicator function 1(demand>0)
summary(as.numeric(demand>0))
Can anyone tell me if this is the right thing to do?
Thank you very much.
Hi!
To help us help you, could you please prepare a repr oducible ex ample (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
AS93
August 9, 2019, 9:16am
3
Unfortunately I could not install the reprex package. Hope it is also traceable with the minimal dataset and minimal runnable code:
df <- data.frame(
demand = c(0.24455661001999, 0.288511028213395, 0.000457341625207,
0.286052311527696, 0.22661840138977),
giniA = c(0.600622095598854, 0.611145511322019, 0.611145511322019,
0.609040828177386, 0.611145511322019)
)
#indicator function 1(.) for demand
summary(as.numeric(df$demand<1))
sd(as.numeric(df$demand<1))
summary(as.numeric(df$demand==1))
sd(as.numeric(df$demand==1))
summary(as.numeric(df$demand>1))
sd(as.numeric(df$demand>1))
#indicator function 1(.) for giniA
summary(as.numeric(df$giniA>0))
sd(as.numeric(df$giniA>0))
summary(as.numeric(df$giniA==0))
sd(as.numeric(df$giniA==0))
Is it right to do this for the creation of an indicator function or is there another way?
Thank you so much for your help.
Sorry but I really don't understand what are you trying to accomplish with the code you are posting, as long as I know, an indicator function simply returns 1 or 0 depending on a defined treshold, like in this example.
library(dplyr)
df <- data.frame(
demand = c(0.24455661001999, 0.288511028213395, 0.000457341625207,
0.286052311527696, 0.22661840138977),
giniA = c(0.600622095598854, 0.611145511322019, 0.611145511322019,
0.609040828177386, 0.611145511322019)
)
df %>%
mutate(demand_indicator = if_else(demand > 0, 1, 0),
giniA_indicator = if_else(giniA > 0, 1, 0))
#> demand giniA demand_indicator giniA_indicator
#> 1 0.2445566100 0.6006221 1 1
#> 2 0.2885110282 0.6111455 1 1
#> 3 0.0004573416 0.6111455 1 1
#> 4 0.2860523115 0.6090408 1 1
#> 5 0.2266184014 0.6111455 1 1
Created on 2019-08-09 by the reprex package (v0.3.0.9000)
system
Closed
August 30, 2019, 5:08pm
5
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.