我是 R 的初學者,我有一個包含兩列的表。第一列是 ID。第二列是標簽(實際上是 CRSP 資料中的 CUSIP)。標簽是八位數字,其中前六位數字標識 ID,最后兩位數字可能因 ID 的某些屬性而異。
我想要一個具有兩個不同標簽的 ID 串列,其中兩個標簽之一以不同的兩位數結尾。
例如,如果表格如下所示,
| ID | 標簽 |
|---|---|
| 1 | 11223330 |
| 1 | 11223341 |
| 2 | 11224430 |
| 3 | 11225530 |
| 3 | 11225531 |
| 4 | 11226630 |
| 5 | 11227730 |
| 5 | 11227753 |
在這種情況下,我希望看到
| ID | 標簽 |
|---|---|
| 1 | 11223330 |
| 1 | 11223341 |
| 3 | 11225530 |
| 3 | 11225531 |
| 5 | 11227730 |
| 5 | 11227753 |
非常感謝您!
uj5u.com熱心網友回復:
這應該這樣做:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
dat <- structure(list(ID = c(1L, 1L, 2L, 3L, 3L, 4L, 5L, 5L), label = c(11223330L,
11223341L, 11224430L, 11225530L, 11225531L, 11226630L, 11227730L,
11227753L)), row.names = c(NA, 8L), class = "data.frame")
dat %>%
distinct() %>%
group_by(ID) %>%
filter(n() >= 2)
#> # A tibble: 6 × 2
#> # Groups: ID [3]
#> ID label
#> <int> <int>
#> 1 1 11223330
#> 2 1 11223341
#> 3 3 11225530
#> 4 3 11225531
#> 5 5 11227730
#> 6 5 11227753
由reprex 包(v2.0.1)于 2022-04-24 創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/463626.html
