连续行数(Number of successive rows)
考虑一个
data.table
,它包含多年来id1
,id2
之间的匹配。id1 year id2 1: 51557094 2003 65122111 2: 51557094 2004 65122111 3: 51557094 2005 65122111 4: 51557094 2007 65122111 5: 51557094 2008 65122111 6: 51557093 2006 65122111
对于这些比赛中的任何一个,我想找出持续时间以及比赛开始的年份。 如果特定年份没有数据,则匹配结束(如果再次有数据,则在下一年开始新匹配)。
因此,对于上面的样本数据,预期的输出将是
id1 year id2 length 1: 51557094 2003 65122111 3 2: 51557094 2007 65122111 2 3: 51557093 2006 65122111 1
我接受了其中一个答案,因为它给我带来了足够的意义,但注意到它不正确。 虽然它适用于样本数据,但不适用于以下情况
> dtId id1 year id2 1: 51557094 2003 65122111 2: 51557094 2004 65122111 3: 51557094 2005 65122111 4: 51557094 2007 65122111 5: 51557094 2008 65122111 6: 51557094 2006 65122112 > setkey(dtId, id1, id2, year) > dtId[,grp := cumsum(c(1,diff(year)) > 1),by=id1] > dtId[,list(year=year[1],length=length(year)),by=list(id1,id2,grp)] id1 id2 grp year length 1: 51557094 65122111 0 2003 5 2: 51557094 65122112 0 2006 1
相反,在
id1, id2
创建匹配变量grp
可以解决问题:> dtId[,grp := cumsum(c(1,diff(year)) > 1),by=list(id1, id2)] > dtId[,list(year=year[1],length=length(year)),by=list(id1,id2,grp)] id1 id2 grp year length 1: 51557094 65122111 0 2003 3 2: 51557094 65122112 0 2006 1 3: 51557094 65122111 1 2007 2
Consider a
data.table
that contains matches betweenid1
,id2
for several years.id1 year id2 1: 51557094 2003 65122111 2: 51557094 2004 65122111 3: 51557094 2005 65122111 4: 51557094 2007 65122111 5: 51557094 2008 65122111 6: 51557093 2006 65122111
For any of these matches, I want to find out the duration, together with the year the match started. If there was no data for a specific year, a match ends (and the next year, if there is data again, a new match starts).
Hence, for the sample data above, the expected output would be
id1 year id2 length 1: 51557094 2003 65122111 3 2: 51557094 2007 65122111 2 3: 51557093 2006 65122111 1
I accepted one of the answers because it brought me far enough, but notice that it was not correct. While it worked for the sample data, it will not for the following
> dtId id1 year id2 1: 51557094 2003 65122111 2: 51557094 2004 65122111 3: 51557094 2005 65122111 4: 51557094 2007 65122111 5: 51557094 2008 65122111 6: 51557094 2006 65122112 > setkey(dtId, id1, id2, year) > dtId[,grp := cumsum(c(1,diff(year)) > 1),by=id1] > dtId[,list(year=year[1],length=length(year)),by=list(id1,id2,grp)] id1 id2 grp year length 1: 51557094 65122111 0 2003 5 2: 51557094 65122112 0 2006 1
Instead, creating the match variable
grp
over bothid1, id2
solves the issue:> dtId[,grp := cumsum(c(1,diff(year)) > 1),by=list(id1, id2)] > dtId[,list(year=year[1],length=length(year)),by=list(id1,id2,grp)] id1 id2 grp year length 1: 51557094 65122111 0 2003 3 2: 51557094 65122112 0 2006 1 3: 51557094 65122111 1 2007 2
原文:https://stackoverflow.com/questions/27283589
最满意答案
在那里你可以看到这里的例子我必须展示如何编码
ajax
响应所以我在我的域名中添加了示例你的
PHP
文件看起来像下面这样,我省略了SQL
部分,并且只添加了关于如何在数组中使用json_encode
的逻辑,希望你发现它有帮助,你可以在你的本地机器上使用这段代码来看看事情是如何工作的<?php $response = array('success'=>false,'likes'=>0); if(isset($_POST['count'])){ $counter = $_POST['count']; $response['likes']=$counter+1; $response['success']=true; } echo json_encode($response); ?>
您的HTML页面如下
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <style> .feed { width: 95%; height: auto; } i.fa { cursor: pointer; } </style> <script type="text/javascript"> $(document).ready(function () { $(".voteup").click(function () { var curElement = $(this); console.log(curElement.parent().find('.likes').text()); $.ajax({ url: 'test.php', dataType: 'json', data: 'count=' + curElement.parent().find(".likes").text(), method: 'POST' }).done(function (data) { if (data.success) { curElement.parent().find(".likes").html(data.likes); } else { alert('Some Error occoured at the server while liking the feed'); } }); }); }); </script> </head> <body> <div class="panel panel-default"> <div class="panel-heading">Panel Heading</div> <div class="panel-body"> <div class="feed"> <p>This is my feed can someone like it</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> <div class="feed"> <p>Another feed item</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> <div class="feed"> <p>This is my feed can someone like it</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> <div class="feed"> <p>This is my feed can someone like it</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> <div class="feed"> <p>This is my feed can someone like it</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> <div class="feed"> <p>This is my feed can someone like it</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> </div> </div> </body> </html>
编辑:
基本上,我只是增加发布的变量
count
你不必做,只需要在数据库中更新喜欢就可以了,一旦你发送了ajax调用,然后用SQL
查询计数并以我用过的相同格式显示输出。关于$.parseJSON()
你会注意到这里使用的ajax调用将dataType
设置为JSON
如果你设置了不需要解析响应的dataType
,否则你应该使用var myData=$.parseJSON(data);
然后像myData.likes
myData.success
一样使用there you go you can see the example here I had to show how the
ajax
response should be encoded so I added the example on my domainyour
PHP
file will look like the following, I have omitted theSQL
part and added only the logic on how to usejson_encode
with the arrays hope you find it helpful you can use this code on your local machine to look into how things are working<?php $response = array('success'=>false,'likes'=>0); if(isset($_POST['count'])){ $counter = $_POST['count']; $response['likes']=$counter+1; $response['success']=true; } echo json_encode($response); ?>
your HTML page is below
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <style> .feed { width: 95%; height: auto; } i.fa { cursor: pointer; } </style> <script type="text/javascript"> $(document).ready(function () { $(".voteup").click(function () { var curElement = $(this); console.log(curElement.parent().find('.likes').text()); $.ajax({ url: 'test.php', dataType: 'json', data: 'count=' + curElement.parent().find(".likes").text(), method: 'POST' }).done(function (data) { if (data.success) { curElement.parent().find(".likes").html(data.likes); } else { alert('Some Error occoured at the server while liking the feed'); } }); }); }); </script> </head> <body> <div class="panel panel-default"> <div class="panel-heading">Panel Heading</div> <div class="panel-body"> <div class="feed"> <p>This is my feed can someone like it</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> <div class="feed"> <p>Another feed item</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> <div class="feed"> <p>This is my feed can someone like it</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> <div class="feed"> <p>This is my feed can someone like it</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> <div class="feed"> <p>This is my feed can someone like it</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> <div class="feed"> <p>This is my feed can someone like it</p> <i class="fa fa-thumbs-up voteup" aria-hidden="true" ></i> <span class="likes">0</span> <i class="fa fa-thumbs-down votedown" aria-hidden="true" ></i> <span class="dlikes">0</span> </div> </div> </div> </body> </html>
EDIT:
Basically, I am just incrementing the posted variable
count
you do not have to do that you just need to update likes in the database once you send the ajax call and then count with anSQL
query and show the output in the same format I have used.And about the$.parseJSON()
you will notice that the ajax call used here has thedataType
set toJSON
if you have set thedataType
you do not need to parse the response otherwise you should usevar myData=$.parseJSON(data);
and then use likemyData.likes
myData.success
相关问答
更多-
要获取属性data-id的内容(如link ),您必须使用 $(this).attr("data-id") // will return the string "123" 或.data() (如果您使用较新的jQuery> = 1.4.3) $(this).data("id") // will return the number 123 而data-后的部分必须是小写的,例如data-idNum将不起作用,但是data-idnum将不会。 To get the ...
-
不知道为什么不能用jquery获取data-id。(Don't know why can't get data-id with jquery. It's always undefined)[2023-10-08]
你正在调用没有任何参数的that.deleteTrack() ,但是deleteTrack需要一个元素。 var id = $(el).attr("data-id"); 失败,因为el未定义 用于演示目的的小提琴: https : //jsfiddle.net/kjb6ug9c/2/ You are calling that.deleteTrack() without any arguments, but deleteTrack expects an element. var id = $(el).attr ... -
因为您总是访问第一个项目 function executeStatus() { $('#btn-footer').click(function(e) { e.preventDefault(); var foo = $(".sorting1").data('id'); alert(foo); }); } 您的函数从第一个匹配“.sorting1”的项中获取data-id。 你可能想试试 $("#id_preview .sorting1").data('id' ...
-
在jQuery中获取data-id(Getting data-id in jQuery)[2023-04-27]
你忘记了引号: // Note the `a` tag's `data-id` is changed from: // data-id='+res.display[i].id+'> // to // data-id="'+res.display[i].id+'"> results += '' + ' '+(i+1)+' ' + ''+res.display[i].name+' ' + ...由于使用jQuery已经很简单,使用closest()和data() $('.like').on('click', function(event) { event.preventDefault(); var revId = $(this).closest('.comment').data('reviewid') .... Since using jQuery already is simple using closest() and data() $('.like').on('cli ...捕获.PQReport上的单击。 把它的data-id放进去.AskForDelete的id。 例如: $(document).ready(function(){ $('.PQReport').click(function(e){ e.preventDefault(); $('.AskForDelete').attr('id', $(this).data('id')) }) $('.AskForDelete').click(function(e) { e. ...javascript:如何从foreach获取data-id上的每个记录ID?(javascript: How to get each record id on data-id from foreach?)[2022-07-26]
元素的id属性在页面上必须是唯一的 。 这意味着不允许有2个或更多具有相同ID的元素。 只有具有此id第一个元素才会被视为真实元素。 你可以使用类来代替。 但在你的情况下,将itemId直接传递给DeleteCategory函数会更容易: 并将其修改为: function DeleteCategory(Id) ...问题是你正在使用数据api和attr。 当您使用数据api获取数据时,它将存储在内部结构中,因此当您使用attr更新data-id ,只更新属性值而不更新数据api存储的内部值。 当你有data-*属性时,它用于初始化数据api的值,但是一旦完成,那些就不会同步。 $('#remove_cv').removeAttr('disabled').data('id', response.document_id); The problem is you are using data api and the att ...在那里你可以看到这里的例子我必须展示如何编码ajax响应所以我在我的域名中添加了示例 你的PHP文件看起来像下面这样,我省略了SQL部分,并且只添加了关于如何在数组中使用json_encode的逻辑,希望你发现它有帮助,你可以在你的本地机器上使用这段代码来看看事情是如何工作的 false,'likes'=>0); if(isset($_POST['count'])){ $counter = $_POST['cou ...相关文章
更多- Java Number类
- javascript replace 连续用两次的问题
- (八) 通过jdbc对对solr进行数据的增量导入
- 使用mybatis执行sql的时候为什么会出现Parameter index out of range (1 > number of parameters, which is 0)?
- Flash图表控件FusionCharts如何在图表标绘非连续数据
- HTML5项目笔记8:使用HTML5 的跨域通信机制进行数据同步
- 使用spring+ibatis时如何在后台进行数据库的备份与还原
- [Hive] 完全分布式安装过程(MetaStore: MySQL)
- solr tip
- solr的分组和统计功能
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)