-6

I have two varaibles a and amount sorted by a

a      amount

112    12000 
112    15000 
113    14000
114    18000
114    17000 
115    19000 
115    17000

I want the first row occurrence of each value in a variable

output 

 a    amount
112  12000
113  14000
114  18000
115  19000 
111111
  • 67
  • 1
  • 1
  • 5
  • 8
    http://stackoverflow.com/questions/34042294/getting-only-first-row-of-data-by-factor-in-r or http://stackoverflow.com/questions/19451032/r-returning-first-row-of-group or http://stats.stackexchange.com/questions/7884/fast-ways-in-r-to-get-the-first-row-of-a-data-frame-grouped-by-an-identifier or http://stackoverflow.com/questions/19424762/efficiently-selecting-top-number-of-rows-for-each-unique-value-of-a-column-in-a or http://stackoverflow.com/questions/13279582/select-only-the-first-rows-for-each-unique-value-of-a-column-in-r – thelatemail May 13 '16 at 05:14

2 Answers2

2

You can use duplicated which would give you the duplicated values. You can ignore them with ! operator

df[!duplicated(df$a), ]


#   a amount
#1 112  12000
#3 113  14000
#4 114  18000
#6 115  19000

Or

you can also use match along with unique

df[match(unique(df$a), df$a), ]

#   a amount
#1 112  12000
#3 113  14000
#4 114  18000
#6 115  19000
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

We can use

library(data.table)
setDT(df1)[, head(.SD, 1), by = a]

Or a fast variant (contributed by @Symbolix)

setDT(df1)[df1[, .I[1L], by = a]$V1]

Or use unique

unique(setDT(df1), by = "a")
#    a amount
#1: 112  12000
#2: 113  14000
#3: 114  18000
#4: 115  19000

Or

library(dplyr)
df1 %>%
    group_by(a) %>%
    slice(1)

Or use summarise with first

df1 %>%
   group_by(a) %>% 
   summarise(amount = first(amount))

Or with base R

aggregate(.~a, df1, head, 1)
#    a amount
#1 112  12000
#2 113  14000
#3 114  18000
#4 115  19000
akrun
  • 874,273
  • 37
  • 540
  • 662