我试图根据值的出现来获取数据帧的子集.这在下面给出的一个例子中得到了最好的解释.这个问题与以下内容有很大关系:为R中数据名称中列的每个唯一值选择最有限行数 但是,我想改变head()命令选择的项目数.
#Sample data
input <- matrix( c(1000001,1000001,1000001,1000001,1000001,1000001,1000002,1000002,1000002,1000003,1000003,1000003,100001,100002,100003,100004,100005,100006,100002,100003,100007,100002,100003,100008,"2011-01-01","2011-01-02","2011-01-01","2011-01-04","2011-01-01","2011-01-02","2011-01-01","2011-01-04","2011-01-01","2011-01-02","2011-01-01","2011-01-04"), ncol=3)
colnames(input) <- c( "Product" , "Something" ,"Date")
input <- as.data.frame(input)
input$Date <- as.Date(input[,"Date"], "%Y-%m-%d")
#Sort based on date, I want to leave out the entries with the oldest dates.
input <- input[ with( input, order(Date)), ]
#Create number of items I want to select
table_input <- as.data.frame(table(input$Product))
table_input$twentyfive <- ceiling( table_input$Freq*0.25 )
#This next part is a very time consuming method (Have 2 mln rows, 90k different products)
first <- …Run Code Online (Sandbox Code Playgroud) 前段时间我提出了一个关于创建市场篮子数据的问题.现在我想创建一个类似的data.frame,但是基于第三个变量.不幸的是我遇到了问题.上一个问题:在
Rshasha和@ SimonO101中创建市场篮子矩阵的有效方法给了我很好的答案,但我无法正确改变他们的anwser.我有以下数据:
Customer <- as.factor(c(1000001,1000001,1000001,1000001,1000001,1000001,1000002,1000002,1000002,1000003,1000003,1000003))
Product <- as.factor(c(100001,100001,100001,100004,100004,100002,100003,100003,100003,100002,100003,100008))
input <- data.frame(Customer,Product)
Run Code Online (Sandbox Code Playgroud)
我现在可以通过以下方式创建列联表:
input_df <- as.data.frame.matrix(table(input))
Run Code Online (Sandbox Code Playgroud)
但是我有一个第三个(数字)变量,我想在表中输出.
Number <- c(3,1,-4,1,1,1,1,1,1,1,1,1)
input <- data.frame(Customer,Product,Number)
Run Code Online (Sandbox Code Playgroud)
现在代码(当然,现在有3个变量)不再起作用了.我正在寻找的结果具有唯一的Customer作为行名称和唯一的Product作为列名称.并且Number为值(如果不存在则为0),此数字可以通过以下公式计算:
input_agg <- aggregate( Number ~ Customer + Product, data = input, sum)
Run Code Online (Sandbox Code Playgroud)
希望我的问题很明确,如果不清楚,请发表评论.
我一直在努力做一些我希望成为一个简单问题的事情.我想从Spotfire输出值(String/Real/Stringlist)到我的计算机上的文本文件.我的最终解决方案需要列出并保存文档属性,但是现在让我们使用stackoverflow中的工作脚本:是否可以使用IronPython将当前*.dxp项目的路径作为字符串返回?
两个变量:路径和分析是"打印"的.如何将它们打印到文本文件中?
类似的东西在这里完成:http://spotfired.blogspot.co.uk/2014/04/export-image-from-visualization.html其中创建.bmp图像.
非常感谢,