Skip to content

Remove rows that have repeated items in a particular column

5 messages · Zhongyi Yuan, Michael Kao, arun

#
Very simple

mat[!duplicated(mat[, 1]), ]

M
On 26/07/12 06:50, Zhongyi Yuan wrote:
#
HI,

Try this:

?mat[!duplicated(mat[,1]),]
???? [,1] [,2]
[1,]??? 1??? 5
[2,]??? 2??? 1
[3,]??? 3??? 4

A.K.



----- Original Message -----
From: Zhongyi Yuan <zhongyi-yuan at uiowa.edu>
To: r-help at r-project.org
Cc: 
Sent: Thursday, July 26, 2012 12:50 AM
Subject: [R] Remove rows that have repeated items in a particular column

Dear R Users,

I apology for not being able to provide an adequately informative subject.
Let me describe my problem with an example.

For a matrix
*? ? (mat <- matrix(c(1,1,2,2,2,3,3, 5,9,1,3,7,4,8), ncol = 2))*
my desired output is
*? ? (desired <- matrix(c(1,2,3, 5,1,4), ncol = 2))*

That is, the first column is numerically grouped and only the first item in
each group is wanted.? The second column is in increasing order within each
group.? My actual data will be of size 10^6 by 100 so I am hoping to solve
this by a simple function.? Thank you very much for your help.

Best,
Zhongyi Yuan

??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
HI,

One more solution to you:
mat1<-data.frame(mat)
?desired<-as.matrix(do.call(rbind,lapply(split(mat1,mat1$X1),function(x) head(x,1))))
?colnames(desired)<-NULL
?desired
? [,1] [,2]
1??? 1??? 5
2??? 2??? 1
3??? 3??? 4
A.K.




----- Original Message -----
From: Zhongyi Yuan <zhongyi-yuan at uiowa.edu>
To: r-help at r-project.org
Cc: 
Sent: Thursday, July 26, 2012 12:50 AM
Subject: [R] Remove rows that have repeated items in a particular column

Dear R Users,

I apology for not being able to provide an adequately informative subject.
Let me describe my problem with an example.

For a matrix
*? ? (mat <- matrix(c(1,1,2,2,2,3,3, 5,9,1,3,7,4,8), ncol = 2))*
my desired output is
*? ? (desired <- matrix(c(1,2,3, 5,1,4), ncol = 2))*

That is, the first column is numerically grouped and only the first item in
each group is wanted.? The second column is in increasing order within each
group.? My actual data will be of size 10^6 by 100 so I am hoping to solve
this by a simple function.? Thank you very much for your help.

Best,
Zhongyi Yuan

??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.