在组

时间:2016-02-23 10:32:12

标签: r data.table

我有data.table

X = data.table(x = c(1,1,1,1,1,2,2,2,2,2), y = c(3,2,1,-1,5,7,4,-2,3,5))

我想仅对一组中负值以上的行进行子集化:

res = data.table(x = c(1,1,1,2,2), y = c(3,2,1,7,4)

从第一组中的五个值开始,我只想获得前三个,因为第四个是负数,而第二个组是相同的。

2 个答案:

答案 0 :(得分:5)

以下是两个选项:

Received: from server
Received: from 
Subject: Formulario Alta Usuario Web
X-PHP-Script: 
Date: Tue, 23 Feb 2016 11:07:52 +0000
From: 
Message-ID: <53692f88fe04a71fd41ca2475611fa99@
X-Mailer: PHPMailer 5.2.14 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: text/html; charset="UTF-8'"
Content-Transfer-Encoding: 8bit
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname - 
X-AntiAbuse: Original Domain - 
X-AntiAbuse: Originator/Caller UID/GID - [500 495] / [47 12]
X-AntiAbuse: Sender Address Domain - 
X-Get-Message-Sender-Via: 
X-Authenticated-Sender: 
X-Source: /usr/bin/php
X-Source-Args: /usr/bin/php /home/public_html/
X-Source-Dir: litrodeenergia.com:/public_html/
Return-Path: 
X-MS-Exchange-Organization-Antispam-Report: IPOnAllowList
X-MS-Exchange-Organization-SCL: -1

或者(可能更有效,因为它避免了X[, .SD[seq_len(which.max(y<0)-1L)], by = x] ):

.SD

答案 1 :(得分:1)

我们也可能

X[, .SD[cummin(sign(y))>0], x]
#   x y
#1: 1 3
#2: 1 2
#3: 1 1
#4: 2 7
#5: 2 4