vendredi 31 juillet 2020

How to check a column in a data table for duplicates within a if statement in R

I have data table named quarter_earnings with the following columns: client_name, store_name, store_key, q1_sales, q2_sales, q3_sales, q4_sales. All businesses have 4 rows total (1 per quarter) per store_name.

We want to be able to check if there are duplicated store_names within a client_name grouping. If there are no duplicates, we will be filtering a graph and labeling it using the store_name values. If there are duplicated store_names within a client, we will be filtering on store_key and using a sort of fusion of the store_name and store_key value in the labels.

There is some additional complicated reasoning behind the logic of separating these two populations out, so I'm not going to be simplifying the problem by just consistently doing it one way. I know how to set up the graphs based on each situation, but I'm struggling with the loop that checks for duplicates. How would this be written?

Below was my initial thought, which is not working:

for(client_name in quarter_earnings){
  for(store_name in quarter_earnings){
    if(n_occur$Freq > 1){
      t$lab = store_name, '' store_key
    }
    else{
      t$lab = store_name
    }
  }
}

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire