[R] extract rows based on column value in a data frame

David Winsemius dwinsemius at comcast.net
Wed Dec 2 21:26:19 CET 2015


> On Dec 2, 2015, at 10:09 AM, Ragia Ibrahim <ragia11 at hotmail.com> wrote:
> 
> Dear Group,
> I have a data frame that such as
> 
> v1   v2    v3    v4
> 1      1      3      6
> 1      1       5     6
> 1       1      8     0
> 1       2      6     1
> 1       2      4     0
> 1       3      4      4
> 1       3      5      4
> 1       3      6      3
> 1       3      7      1
> 
> 2       4      3      7
> 2       5       5     4
> 2       5      8     2
> 2       1      6     1
> 2       1      4     0
> 2      1      4      3
> 2       1      5      2
> 3       1      6      1
> 3       6      7      0
> 
> 3      6       3      6
> 3      6       5      6
> 3      6       8      0
> 3      6       6      1
> 3      2       4      0
> 3      2       4      4
> 3      2       5      4
> 3      2       6      3
> 3      2       7      1
> 3       5       9      5
> 3       6       9       5   
> 
> 
> the result required is each first 3 rows,  from distinct v2 column for each v1 column
> 
> 
> v1   v2    v3    v4
> 1      1      3      6
> 1       2      6     1
> 1       3      4      4
> 2       4      3      7
> 2       5       5     4
> 2       1      6     1
> 3      6       3      6
> 3      2       4      0
> 3       5       9      5


Probably something along the lines of 

dfrm[ ave(dfrm$v1, dfrm$v1, FUN=seq_along) %in% 1:3 , ] 


> 
> 
> thanks in advance 		 	   		  
> 	[[alternative HTML version deleted]]

Future postings should be in plain text.

— 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list