Reputation: 1435
I want to select unique values based on user selection from the column header. This is currently under my reactive function.
Example:
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 Name
John 52
Sally 44
or if user selects Last Name
, then:
Smith 64
Gold 32
Upvotes: 1
Views: 85
Reputation: 66
Just saw the desired output you would like, here is some updated code for that:
NameArray<-data.frame(Name= c("John", "Sally", "John"), Income = c(20,44,32), LastName = c("Smith", "Smith","Gold"))
IncomeArray<-function(SearchColumn,SearchText){
#Name of column, Persons name/income.
dims<-which(NameArray[,deparse(substitute(SearchColumn))]==deparse(substitute(SearchText)))
print(NameArray[dims,names(NameArray)!=(deparse(substitute(SearchColumn)))])
}
IncomeArray(Name,John) #Search 'Name' column for 'John' and print out corresponding information.
Income LastName
1 20 Smith
3 32 Gold
I have left the original code below for completeness
NameArray<-data.frame(Name= c("John", "Sally", "John"), Income = c(20,44,32), LastName = c("Smith", "Smith","Gold"))
IncomeArray<-function(SearchColumn,SearchText, ResultColumn){
#Name of column, Persons name/income, Column name of information of interest.
dims<-which(NameArray[,deparse(substitute(SearchColumn))]==deparse(substitute(SearchText)))
print(NameArray[dims,deparse(substitute(ResultColumn))])
}
IncomeArray(Name,John,Income) #Search 'Name' column for 'John' and print out each 'John''s income.
[1] 20 32
The above function might help you out in the cases where you have all the information and just need to access the required columns - little basic but it gets the job done.
Upvotes: 0
Reputation: 160407
Early attempt, using dplyr
:
dat <- data.frame(Name = c("John","Sally", "John"),
Income = c(20, 44, 32),
LastName = c("Smith", "Smith", "Gold"),
stringsAsFactors = FALSE)
library(dplyr)
I'll assume that input
is a list provided by some interface such as shiny
.
This first batch does no summarizing, just a simple "first record":
input <- list(fromUser = "Name")
dat %>%
group_by_(input$fromUser) %>%
slice(1)
# Source: local data frame [2 x 3]
# Groups: Name [2]
# Name Income LastName
# <chr> <dbl> <chr>
# 1 John 20 Smith
# 2 Sally 44 Smith
input <- list(fromUser = "LastName")
dat %>%
group_by_(input$fromUser) %>%
slice(1)
# Source: local data frame [2 x 3]
# Groups: LastName [2]
# Name Income LastName
# <chr> <dbl> <chr>
# 1 John 32 Gold
# 2 John 20 Smith
If you want something different, insert a sort or max or sum or such:
input <- list(fromUser = "LastName")
dat %>%
group_by_(input$fromUser) %>%
summarize(Income = sum(Income))
# # A tibble: 2 x 2
# LastName Income
# <chr> <dbl>
# 1 Gold 32
# 2 Smith 64
Upvotes: 1
Reputation: 206167
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()
sel<-"Name"
aggregate(reformulate(sel, "Income"), dd, sum)
# Name Income
# 1 John 52
# 2 Sally 44
sel<-"LastName"
aggregate(reformulate(sel, "Income"), dd, sum)
# LastName Income
# 1 Gold 32
# 2 Smith 64
Upvotes: 3