1

The following is a simple version of my data:

sample dataset

I want to create a flag for each group if they at least have one item in Column1. I know I can do this in dplyr and then merge it with my original data but I was wondering if there is an easier way.

For example, I can do this:

df_column <- df %>% filter(!is.na(Column1)) %>% group_by(Group)%>%
  summarize(n=n_distinct(Column1))

and then I can merge this with the original data and create a flag.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Mel
  • 157
  • 9
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Don't post data as images because we can't copy/paste it to test it. – MrFlick Feb 14 '20 at 17:31

2 Answers2

2

Without filtering, we can do this with mutate by creating a logical column based on the number of unique elements (n_distinct) in 'Column1' after groupingby 'Group'

library(dplyr)
df %>%
     group_by(Group) %>%
     mutate(flag = n_distinct(Column1[!is.na(Column1)]) > 1)
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Just a riff on the previous answer using ifelse which may be easier to understand if you're coming from Excel (which it looks like you may be):

library(dplyr)

df %>%
    group_by(Group) %>% 
    mutate(flag = ifelse(
        is.na(column1),
        "flag",
        "dont_flag"
    ))
Ben G
  • 4,148
  • 2
  • 22
  • 42