[R] Keep only those values in a row in a data frame which occur only once.

S Ellison S.Ellison at LGCGroup.com
Mon Jun 12 15:54:18 CEST 2017


> I have a file data.txt as follows:
> 
> Name_1,A,B,C
> Name_2,E,F
> Name_3,I,J,I,K,L,M
> 
> My query is how can I keep only the unique elements in each row? For
> example: I want the row 3 to be Name_3,I,J,K,L,M
> 
> Please note I don't want the 2nd I to appear.
> 
> How can I do this?
Use unique() on each row and pad with NA?

Example:
uniq10 <- function(x, L=10) {
	u <- unique(x)
	c(u, rep(NA, L-length(u)) )
}

as.data.frame(  t( apply(tmp, 1, uniq10)  )  )

assuming tmp is the name of your initial data frame.

S Ellison




*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list