0

I am looking to find the unique values with the each row of a column.

df <- as.data.frame(rbind(c('10','20','30','10','45','34'),
                          c('a','b','c','a','b'),
                          c("fs","pp","dd","dd")))

df$f7 <-paste0(df$V1,
               ',',
               df$V2,
               ',',
               df$V3,',',df$V4,',',df$V5,',',df$V6)   

df_1 <- as.data.frame(df[,c(7)])
names(df_1)[1] <-"f1"

The expected output is : Row1 :10,20,30,45,34 Row2: a,b,c Row3:fs,pp,dd

Any help is highly appreciated.

Regards, R

R Ban
  • 97
  • 10

1 Answers1

0

We can loop over the rows with apply (MARGIN = 1 - for rowwise loop), get the unique values and paste

apply(df, 1, FUN = function(x) toString(unique(x)))
akrun
  • 874,273
  • 37
  • 540
  • 662