JamAndJammies JamAndJammies - 1 month ago 5x
R Question

Selecting unique values using user input

I want to select unique values based on user selection from the column header. This is currently under my reactive function.


Name Income Last Name
John 20 Smith
Sally 44 Smith
John 32 Gold

So there's 2 Johns.

I want something like this:

selItems <- c(input$fromUser, "Income")
unique(selItems$input$fromUser) #totally not correct but this is what I am looking to do

As you can see the user can select all Last names that is unique too. So for all Smiths, how much do they make?

Desired Output
if user selects

John 52
Sally 44

or if user selects
Last Name
, then:

Smith 64
Gold 32


Basically you are looking to do some form of aggregation rather than removing duplicates. You have to tell R how to combine redundant rows. The easiest way to do with would be with aggregate. here's an example. First, a more reproducible version of your sample input

dd<-read.table(text="Name    Income    LastName
John    20        Smith
Sally   44        Smith
John    32        Gold", header=T, stringsAsFactors=F)

Then you can use reformualte to build the formulas for use with aggregate()

aggregate(reformulate(sel, "Income"), dd, sum)
#    Name Income
# 1  John     52
# 2 Sally     44    

aggregate(reformulate(sel, "Income"), dd, sum)
#   LastName Income
# 1     Gold     32
# 2    Smith     64