唯一排序的行从R data.table单列 [英] Unique sorted rows single column from R data.table

查看:183
本文介绍了唯一排序的行从R data.table单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小应用程式,列出客户,项目和销售。我想从表中生成一个查找列表,但得到两列,如果我尝试使用keyby命令列表。这可能是非常简单,但我很困惑。

  library(data.table)

company = c ,S,W,L,T,T,W,A,T,W)
item = c Thingy,Widget,Thingy,Grommit,Thingy,Grommit,Thingy,Widget,Thingy)
sales = c(120,140,​​160,180,200,120,140,​​160,180,200)
salesdt< -data.table(company,item,sales)

server< - function(input,output){

输出$ theCustomersList< - renderUI b $ b list(
selectInput(customer,Choose a customer:,
choices = salesdt [,unique(company),keyby = company]
,selectize = FALSE
,selected =A



})

输出$ result< - renderTable(
salesdt [公司%在%c(输入$ customer),
。(valuesold = sum(销售)),项目
]


}
b $ b ui< - fluidPage(
sidebarLayout(
sidebarPanel(
uiOutput(theCustomersList)
),


mainPanel (tableOutput('result'))



shinyApp(ui = ui,server = server)

澄清...我的问题是如何更改

  salesdt [,unique(company),keyby = company] 

解决方案

OP没有指定他希望公司名称显示在select中的特定顺序。 Symbolix的答案是完美的,如果你想要公司名称按字母顺序排序(不需要使用 keyby = 为此目的)。



您还可以按行数排序公司名称(最重要的是首先)

  salesdt [,。 N,by = company] [order(-N),company] 
[1]WTASL
pre>

或总销量(再次,最重要的是第一)

  salesdt [,sum(sales),by = company] [order(-V1),company] 
[1]WTALS

在这两种情况下,都可以通过两个步骤完成:(1)计算每家公司的指标 by = ,(2)根据需要排序结果,但仅返回公司名称。


I have a little app that lists customers, items and sales. I want to generate a lookup list from the table but get two columns if I try to order the list using keyby. It is probably VERY simple but I'm confused. Code below....

library(data.table)

company=c("A","S","W","L","T","T","W","A","T","W")
item=c("Thingy","Thingy","Widget","Thingy","Grommit","Thingy","Grommit","Thingy","Widget","Thingy")
sales=c(120,140,160,180,200,120,140,160,180,200)
salesdt<-data.table(company,item,sales)

server <- function(input, output) {

  output$theCustomersList <- renderUI({
    list(
      selectInput("customer", "Choose a customer:",
                   choices = salesdt[,unique(company), keyby=company]
                   ,selectize=FALSE
                   ,selected="A"
                   )

    )
  })

  output$result <- renderTable(
                    salesdt[company%in%c(input$customer),
                    .(valuesold=sum(sales)), item
                    ]
                  )

}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      uiOutput("theCustomersList")
    ),


    mainPanel(tableOutput('result'))
  )
)

shinyApp(ui = ui, server = server)

To clarify.... my question is how do I change

salesdt[,unique(company), keyby=company]

to get get one column but ordered.

解决方案

The OP didn't specify in which particular order he wants the company names to show up in the select. The answers given by Symbolix are perfect if you want the company names ordered alphabetically (no need to use keyby = for this purpose).

You can also order the company names by the number of rows (most important first)

salesdt[, .N, by = company][order(-N), company]
[1] "W" "T" "A" "S" "L"

or by total sales volume (again, most important first)

salesdt[, sum(sales), by = company][order(-V1), company]
[1] "W" "T" "A" "L" "S"

In both cases, this is done in two steps: (1) Compute the metric (either count or sum) per company using by =, (2) order the result as desired but return only the company names.

这篇关于唯一排序的行从R data.table单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆