Skip to content
Prev 30631 / 398506 Next

making a dataframe out of lapply() result

Remko Duursma wrote:
If you're always trying to get the unique rows, then just use unique():

 > tester
   groups one two
1      A   1   6
2      A   1   6
3      B   2   7
4      B   2   7
5      C   3   8
6      C   3   8
 > unique(tester)
   groups one two
1      A   1   6
3      B   2   7
5      C   3   8

Or use do.call("rbind", ...)

 > do.call("rbind", lapply(split(tester, tester$group),
+  function(x) x[1, ]))
   groups one two
A      1   1   6
B      2   2   7
C      3   3   8
 >

Or just for fun:

 > tester[which(!duplicated(tester$groups)), ]
   groups one two
1      A   1   6
3      B   2   7
5      C   3   8


Regards,
Sundar