Home > front end >  Change value based on an "if then" statement in r
Change value based on an "if then" statement in r

Time:10-24

I have a data set with this set up:

id color
a blue
a blue
a blue
a blue
b green
b green

I would like to rename color based on number of id entries. For example, if n > 3, then pink. I would like the output to look like this:

id color
a pink
a pink
a pink
a pink
b green
b green

I have primarily been using "ready made" codes, if you will, and have just started attempting to write my own codes to accomplish things, albeit fairly unsuccessfully. This is the code I have attempted to write, but alas, it doesn't work:

df <- df %>% group_by(id) %>% if(n() > 3){print("pink")}

If it is pertinent, color is a factor.

CodePudding user response:

You could use

library(dplyr)

df %>% 
  group_by(id) %>% 
  mutate(color = ifelse(n() > 3, "pink", color)) %>%
  ungroup()

This returns

# A tibble: 6 x 2
  id    color
  <chr> <chr>
1 a     pink 
2 a     pink 
3 a     pink 
4 a     pink 
5 b     green
6 b     green

If there are more colors to add based on a count, use case_when instead:

df %>% 
  group_by(id) %>% 
  mutate(color = case_when(n() > 1 & n() <= 3 ~ "brown",
                           n() > 3 ~ "pink",
                           TRUE ~ color)) %>%
  ungroup()

CodePudding user response:

We may use if/else within in the mutate

library(dplyr)
df %>%
    group_by(id) %>%
    mutate(color = if(n() > 3) "pink" else color) %>%
    ungroup

-output

# A tibble: 6 × 2
  id    color
  <chr> <chr>
1 a     pink 
2 a     pink 
3 a     pink 
4 a     pink 
5 b     green
6 b     green

Or using base R

df$color[with(df, id %in% names(which(table(id) > 3)))] <- "pink"

data

df <- structure(list(id = c("a", "a", "a", "a", "b", "b"), color = c("blue", 
"blue", "blue", "blue", "green", "green")), 
class = "data.frame", row.names = c(NA, 
-6L))
  •  Tags:  
  • r
  • Related