首页 \ 问答 \ 连续行数(Number of successive rows)

连续行数(Number of successive rows)

考虑一个data.table ,它包含多年来id1id2之间的匹配。

        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 between id1, 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 both id1, 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
更新时间:2022-09-17 08:09

最满意答案

在那里你可以看到这里的例子我必须展示如何编码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 domain

your PHP file will look like the following, I have omitted the SQL part and added only the logic on how to use json_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 an SQL 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 the dataType set to JSON if you have set the dataType you do not need to parse the response otherwise you should use var myData=$.parseJSON(data); and then use like myData.likes myData.success

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用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)