使用data.table删除R中的行 [英] Remove rows in R using data.table

查看:259
本文介绍了使用data.table删除R中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

df:-

Date    Name  Salary 
Q1 2015 ABC   $10
Q2 2015 ABC   $11
Q3 2015 ABC   $15
Q1 2015 XYZ   $25
Q2 2015 XYZ   $20

我想从总频率小于3的数据中删除行。 XYZ的频率为2,所以我想删除第4行和第5行。

I want to remove the rows from the data whose total frequency is less than 3. For e.g. XYZ have a frequency of 2 and so I want to remove row 4 and 5.

test <- setDT(df)[,.I[.N>2],by=Name]

输出:-

> test
   Name V1
1:  ABC  1
2:  ABC  2
3:  ABC  3

过滤正确完成,但是我没有得到整个数据集,我只得到输出中的Name列。

Filtering is done correctly but I don't get the whole data set, I only get the Name column in the output.

推荐答案

我们需要提取 V1列并将其用作 i中的行索引来对行进行子集化。 / p>

We need to extract the 'V1' column and use it as row index in 'i' to subset the rows.

setDT(df)[df[,.I[.N>2],by=Name]$V1]
#       Date Name Salary
#1: Q1 2015  ABC    $10
#2: Q2 2015  ABC    $11
#3: Q3 2015  ABC    $15






或者带有 if 和<$的简洁选项c $ c> .SD

setDT(df)[, if(.N >2) .SD, by = Name]
#    Name    Date Salary
#1:  ABC Q1 2015    $10
#2:  ABC Q2 2015    $11
#3:  ABC Q3 2015    $15






以防万一,如果我们需要 dplyr 方法

library(dplyr)
df %>%
   group_by(Name) %>%
   filter(n() >2 )
#      Date  Name Salary
#     <chr> <chr>  <chr>
#1 Q1 2015   ABC    $10
#2 Q2 2015   ABC    $11
#3 Q3 2015   ABC    $15






或者使用 base R ,我们可以有多种选择,其中一种是 ave


Or with base R, we can have a number of options, one with ave

df[with(df, ave(seq_along(Name), Name, FUN = length)>2),]

或使用 table

tbl <- table(df$Name)> 2
subset(df, Name %in% names(tbl)[tbl])

这篇关于使用data.table删除R中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆