-1

Is there a way to populate reactive column in a dataframe. Example

df
A   B   
24  27  
24  25  
25  15  
29  19  
13  22  
39  28  

Expected output (based on values in column B. If the value is less than 25, less, or else More).

df1
A   B   C
24  27  More
24  25  More
25  15  Less
29  19  Less
13  22  Less
39  28  More

Can we populate column C above reactively based on values in column B

manish p
  • 177
  • 6

2 Answers2

1

You can use the condition df$B < 25 to select form a vector holding More and Less.

df$C <- c("More", "Less")[1 + (df$B < 25)]
df
#   A  B    C
#1 24 27 More
#2 24 25 More
#3 25 15 Less
#4 29 19 Less
#5 13 22 Less
#6 39 28 More
GKi
  • 37,245
  • 2
  • 26
  • 48
0

Using tidyverse you could use mutate. df %>% mutate(C=ifelse(B<25, "Less", "More")) -> df1

Yves Cavalcanti
  • 339
  • 2
  • 5