Guisseppe
Guisseppe

Reputation: 3

Get row index in repetitive values in Data Frame

I have a large Dataset (50 000 rows) with data similar to this:

CODE        sYS     YEAR  MONTH VAR STATION
00000539    BREAK   1998    12   n  HUAYAN
00000539    BREAK   2003    12   n  HUAYAN
00000539    BREAK   2008    12   n  HUAYAN
00000539    BREAK   2009    12   n  HUAYAN
00000539    BREAK   2015    12   n  HUAYAN
00000543    BREAK   1992    12   n  NANA
00000543    BREAK   2008    12   n  NANA
00000543    BREAK   2010    12   n  NANA
00000638    BREAK   1971    12   n  PACARAN
00000638    BREAK   1973    12   n  PACARAN
00000638    BREAK   1997    12   n  PACARAN
00000727    BREAK   1973    12   n  COPARA
00000727    BREAK   1995    12   n  COPARA
00000727    BREAK   1997    12   n  COPARA
00000727    BREAK   1998    12   n  COPARA

What i want is to get the row index of specific years like e.g.

x <- c(1973, 1998, 2008)

I tried this:

> row_index <- match(x, DataSet$Year)
> print(row_index)
> 10 1 3

As can you see with "match()" i only get the first value and not all, because what i expected i like this:

> 10 12 1 15 3 7

Any advice or help. Thanks.

Upvotes: 0

Views: 45

Answers (2)

Shree
Shree

Reputation: 11140

How about which -

row_index <- which(DataSet$Year %in% x)

Upvotes: 0

Jilber Urbina
Jilber Urbina

Reputation: 61154

> with(DataSet, YEAR[duplicated(YEAR) & duplicated(STATION)])
[1] 2008 1997 1998

Upvotes: 1

Related Questions