我想根据列标题中的用户选择选择唯一值。目前这是我的反应函数。
示例:
Name Income Last Name
John 20 Smith
Sally 44 Smith
John 32 Gold
所以有两个约翰。
我想要这样的事情:
selItems <- c(input$fromUser, "Income")
unique(selItems$input$fromUser) #totally not correct but this is what I am looking to do
正如您所看到的,用户可以选择所有唯一的姓氏。所以对于所有史密斯来说,他们做了多少?
期望的输出
如果用户选择Name
John 52
Sally 44
或如果用户选择Last Name
,则:
Smith 64
Gold 32
答案 0 :(得分:3)
基本上,您希望进行某种形式的聚合,而不是删除重复项。你必须告诉R如何组合冗余行。最简单的方法是使用聚合。这是一个例子。首先,您的样本输入的更可重现版本
dd<-read.table(text="Name Income LastName
John 20 Smith
Sally 44 Smith
John 32 Gold", header=T, stringsAsFactors=F)
然后,您可以使用reformualte
构建用于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
答案 1 :(得分:1)
尽早尝试,使用dplyr
:
dat <- data.frame(Name = c("John","Sally", "John"),
Income = c(20, 44, 32),
LastName = c("Smith", "Smith", "Gold"),
stringsAsFactors = FALSE)
library(dplyr)
我假设input
是某个界面提供的列表,例如shiny
。
第一批没有总结,只是一个简单的“第一条记录”:
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
如果您想要不同的东西,请插入排序或最大值或总和或等:
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
答案 2 :(得分:0)
只是看到了您想要的所需输出,这里有一些更新的代码:
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
我已将原始代码保留为完整性
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
上述功能可能会帮助您解决所有信息并且只需要访问所需列的情况 - 很少基本但是可以完成工作。