Dear R users,
I'd like to make this data
rem.y = c(-1,0,2,4,5)
from
y = c(-1,-1,0,2,2,2,2,4,4,5,5,5,5,5).
That is, I need to remove repeated values.
Here is my code, but I don't think it is efficient. How could I improve
this?
#------------------------------------------------------------------------
y = c(-1,-1,0,2,2,2,2,4,4,5,5,5,5,5)
n=length(y)
for (i in 1:n) # removed same values in y
{
imsi = 0
if (i==1) {rem.y = y[i]}
else {c = length(rem.y)
for (j in 1:c) { if (y[i]==rem.y[j]) imsi=1 }
if (imsi==0) rem.y = c(rem.y,y[i])}
}
rem.y
#-------------------------------------------------------------------------
Any suggestion will be greatly appreciated.
Regards,
Kathryn Lord
--
View this message in context:
http://www.nabble.com/Remove-repeated-values-tp19885503p19885503.html
Sent from the R help mailing list archive at Nabble.com.
Dear Kathie, See ?unique. y = c(-1,-1,0,2,2,2,2,4,4,5,5,5,5,5) unique(y) [1] -1 0 2 4 5 HTH, Jorge On Wed, Oct 8, 2008 at 3:12 PM, kathie <kathryn.lord2000@gmail.com> wrote:> > Dear R users, > > I'd like to make this data > > rem.y = c(-1,0,2,4,5) > > from > > y = c(-1,-1,0,2,2,2,2,4,4,5,5,5,5,5). > > That is, I need to remove repeated values. > > > Here is my code, but I don't think it is efficient. How could I improve > this? > > > #------------------------------------------------------------------------ > y = c(-1,-1,0,2,2,2,2,4,4,5,5,5,5,5) > n=length(y) > > for (i in 1:n) # removed same values in y > { > imsi = 0 > if (i==1) {rem.y = y[i]} > else {c = length(rem.y) > for (j in 1:c) { if (y[i]==rem.y[j]) imsi=1 } > if (imsi==0) rem.y = c(rem.y,y[i])} > } > > rem.y > #------------------------------------------------------------------------- > > > Any suggestion will be greatly appreciated. > > Regards, > > Kathryn Lord > -- > View this message in context: > http://www.nabble.com/Remove-repeated-values-tp19885503p19885503.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
kathie wrote:> Dear R users, > > I'd like to make this data > > rem.y = c(-1,0,2,4,5) > > from > > y = c(-1,-1,0,2,2,2,2,4,4,5,5,5,5,5). > > That is, I need to remove repeated values. > > > Here is my code, but I don't think it is efficient. How could I improve > this? > >By using the 'unique' function, as in, rem.y <- unique(y). Almost any time you use subscripts, you should ask yourself if there's a vectorized function already available.> #------------------------------------------------------------------------ > y = c(-1,-1,0,2,2,2,2,4,4,5,5,5,5,5) > n=length(y) > > for (i in 1:n) # removed same values in y > { > imsi = 0 > if (i==1) {rem.y = y[i]} > else {c = length(rem.y) > for (j in 1:c) { if (y[i]==rem.y[j]) imsi=1 } > if (imsi==0) rem.y = c(rem.y,y[i])} > } > > rem.y > #------------------------------------------------------------------------- > > > Any suggestion will be greatly appreciated. > > Regards, > > Kathryn Lord
Kathryn ?unique and see also duplicated.> unique(c(-1,-1,0,2,2,2,2,4,4,5,5,5,5,5))[1] -1 0 2 4 5 HTH .... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of kathie > Sent: Thursday, 9 October 2008 8:12 a.m. > To: r-help at r-project.org > Subject: [R] Re move repeated values > > > Dear R users, > > I'd like to make this data > > rem.y = c(-1,0,2,4,5) > > from > > y = c(-1,-1,0,2,2,2,2,4,4,5,5,5,5,5). > > That is, I need to remove repeated values. > > > Here is my code, but I don't think it is efficient. How > could I improve this? > > > #------------------------------------------------------------- > ----------- > y = c(-1,-1,0,2,2,2,2,4,4,5,5,5,5,5) > n=length(y) > > for (i in 1:n) # removed same values in y > { > imsi = 0 > if (i==1) {rem.y = y[i]} > else {c = length(rem.y) > for (j in 1:c) { if (y[i]==rem.y[j]) imsi=1 } > if (imsi==0) rem.y = c(rem.y,y[i])} } > > rem.y > #------------------------------------------------------------- > ------------ > > > Any suggestion will be greatly appreciated. > > Regards, > > Kathryn Lord > -- > View this message in context: > http://www.nabble.com/Remove-repeated-values-tp19885503p19885503.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >The contents of this e-mail are privileged and/or confidential to the named recipient and are not to be used by any other person and/or organisation. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail.