0

I have two columns, first is the function of the Business (Upstream, Downstream , Midstream etc) and the other is the city those functions operate in. There are a ton of other functions amd cities but I have made it smaller here and it looks something like this:

Function      City
DownStream   Edmonton
Downstream   Edmonton
upstream     Edmonton
Upstream     Calgary
Midstream    Calgary
Midstream    Calgary
Midtream     Edmonton
Upstream     Vancouver

I know the unique() function will return the unqiue values I am looking for, however I am having trouble creating something that will group the Function column and then listing the unique Cities for each Function.

Something like this as the end results:

Functions  City

Downstream Edmonton


Midstream  Edmonton 
           Calgary 


Upstream   Edmonton
           Calgary
           Vancouver

Here is some sample data to work from

# Create sample data
Function=c("DownStream", "DownStream", "Upstream", "Upstream", "Midstream", 
           "Midstream", "Midstream", "Upstream")
City=c("Edmonton", "Edmonton", "Edmonton", "Calgary","Calgary", "Calgary",
       "Edmonton", "Vancouver")
df <- data.frame(Function, City)
MatAff
  • 1,250
  • 14
  • 26
Xin
  • 666
  • 4
  • 16
  • 1
    Possible duplicate of [unique() for more than one variable](https://stackoverflow.com/questions/7790732/unique-for-more-than-one-variable) – DanTan Feb 07 '19 at 16:44
  • Are you looking for an output where the grouping variable only appears once? Or do you want a `data.frame` with the unique combinations? – Douglas Mesquita Feb 07 '19 at 16:46
  • Each Functions can appear only once or more, but each unique cities should appear once for each Functions – Xin Feb 07 '19 at 16:48
  • Is the inconsistent naming and capitalization intentional? – MatAff Feb 07 '19 at 17:36

2 Answers2

1

I would suggest first getting the unique rows and than replacing the duplicates with NA.

# Create sample data
Function=c("DownStream", "DownStream", "Upstream", "Upstream", "Midstream", 
           "Midstream", "Midstream", "Upstream")
City=c("Edmonton", "Edmonton", "Edmonton", "Calgary", "Calgary", "Calgary", 
       "Edmonton", "Vancouver")
df <- data.frame(Function, City)

# Get unique rows and replace duplicates with NA
unique_df <- unique(df)
unique_df[duplicated(unique_df[,"Function"]),"Function"] <- NA
print(unique_df)
MatAff
  • 1,250
  • 14
  • 26
0

First create a data frame in R, like df=data.frame(Function, City), then simply unique(Function, City) will give the results.
Or use dplyr package, then run the following codes df %>% group_by(Function) %>% distinct(City).

Kate
  • 1