简体   繁体   中英

Creating a conditional variable in r

We've been given an assignment where we have to create 2 variables with uniform distributions between 0 and 1, Luck and Intelligence, with 2000 observations:

Intelligence <- runif(2000, 0, 1)
Luck <- runif <- runif(2000, 0, 1)

We then have to create a University variable, where University = 1 if Luck + Intelligence > 1 , and University = 0 otherwise.

My instinct is then to first create a function:

University2 <- function(a = Intelligence, b = Luck, nPoints = 2000){
  y = a + b
  return(y)
}

It has 2000 observations, and defines y as a +b, where a = intelligence and b = luck. I am not all that experienced with R, so my problem is the condition for my final "University" value, where I need to tell R: University = 1 , if a + b > 1.

University <- floor(Intelligence+Luck)

你的意思是:

University <- ifelse(Intelligence+Luck > 1, 1, 0)

Using dplyr will streamline your workflow and make your code easier to read, I would suggest case_when (works just like the SQL CASE WHEN which is incredibly useful);

library(tidyverse)

# make a tibble of your data
df <- data.frame(Intelligence, Luck)
df <- as.tibble(df)

df <- 
  df %>% 
  # new column, adds intelligence + Luck
  mutate (intelligence_luck_sum = Intelligence + Luck) %>% 
  # combine 'case_when' and 'mutate', create new variable 'University'
  # and perform the case when test for 2 scenarios
  mutate ( 
    University = case_when(
      intelligence_luck_sum > 1 ~ '1'
      , intelligence_luck_sum < 1 ~ '0')) %>% 
  # remove the 'intelligence' variable
  select(-intelligence_luck_sum)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM