是否有比以下更有效的查询
DT[, list(length(unique(OrderNo)) ),customerID]
Run Code Online (Sandbox Code Playgroud)
使用客户ID,订单号和产品系列项细化LONG格式表,这意味着如果客户在该交易中购买了多个项目,则会有重复的行具有相同的订单ID.
试图找出独特的购买方式.length()按客户ID计算所有订单ID,包括重复项,仅查找唯一编号.
这是一些虚拟代码.理想情况下,我正在寻找的是使用第一个查询的输出unique().
df <- data.frame(
customerID=as.factor(c(rep("A",3),rep("B",4))),
product=as.factor(c(rep("widget",2),rep("otherstuff",5))),
orderID=as.factor(c("xyz","xyz","abd","qwe","rty","yui","poi")),
OrderDate=as.Date(c("2013-07-01","2013-07-01","2013-07-03","2013-06-01","2013-06-02","2013-06-03","2013-07-01"))
)
DT.eg <- as.data.table(df)
#Gives unique order counts
DT.eg[, list(orderlength = length(unique(orderID)) ),customerID]
#Gives counts of all orders by customer
DT.eg[,.SD, keyby=list(orderID, customerID)][, .N, by=customerID]
^
|
This should be .N, not .SD ~ R.S.
Run Code Online (Sandbox Code Playgroud) 使用以下数据集,如何编写一个data.table调用,该子集对此表进行子集并返回该客户的所有客户ID和相关订单,如果客户购买了SKU 1?
预期结果应该返回一个表格,该表格在该条件下排除cid 3和5,并且匹配sku的客户的每一行== 1.
我因为不知道怎么写"包含"语句而陷入困境,== literal只返回sku的匹配条件...我相信有更好的方法..
library("data.table")
df<-data.frame(cid=c(1,1,1,1,1,2,2,2,2,2,3,4,5,5,6,6),
order=c(1,1,1,2,3,4,4,4,5,5,6,7,8,8,9,9),
sku=c(1,2,3,2,3,1,2,3,1,3,2,1,2,3,1,2))
dt=as.data.table(df)
Run Code Online (Sandbox Code Playgroud) 如何通过运行带有闪亮和rchart的多个图表来覆盖输出显示选项,以便输出结果为2x2矩阵类型的网格布局.
require(rCharts)
require(shiny)
require(data.table)
runApp(list(
ui = mainPanel( span="span6",
showOutput("chart2", "Highcharts"),
showOutput("chart3", "Highcharts"),
showOutput("chart4", "Highcharts")
),
server = function(input, output){
output$chart3 <- renderChart({
a <- hPlot(Pulse ~ Height, data = MASS::survey, type = "bubble", title = "Zoom demo", subtitle = "bubble chart", size = "Age", group = "Exer")
a$chart(zoomType = "xy")
a$chart(backgroundColor = NULL)
a$set(dom = 'chart3')
return(a)
})
output$chart2 <- renderChart({
survey <- as.data.table(MASS::survey)
freq <- survey[ , .N, by = c('Sex', 'Smoke')]
a <- hPlot(x = 'Smoke', y …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个函数,或者甚至只是计算如何使用data.table语法运行循环,我可以通过因子对表进行子集化,在本例中为id变量,然后在每个子集上运行线性模型并输出结果.以下示例数据.
df <- data.frame(id = letters[1:3],
cyl = sample(c("a","b","c"), 30, replace = TRUE),
factor = sample(c(TRUE, FALSE), 30, replace = TRUE),
hp = sample(c(20:50), 30, replace = TRUE))
dt=as.data.table(df)
fit <- lm(hp ~ cyl + factor, data = df) #how do I get the [i] to work here to subset and iterate by each factor and also do it in data.table syntax?
Run Code Online (Sandbox Code Playgroud)
预期的结果是适合[1]模型,拟合[2]模型等.
我有一个data.table查询的通用表单,我可以将我的数据子集化,只查看与%like%语句匹配的值,它看起来像
DT[Var %like% "x|y|z", .N,]
Run Code Online (Sandbox Code Playgroud)
并为一般运营商排除价值
i = x != "somevalue",
Run Code Online (Sandbox Code Playgroud)
如何将这些值组合起来忽略听起来像%某些值的值,并且仅返回与这些请求不匹配的集合.
这里的上下文是一个庞大的客户数据数据库,并试图删除不需要的数据,因此这个列表比我感兴趣的列表小得多.