Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

From the following data frame, I want to get unique elements from the f1 column based on the f2 column.

df <- as.data.frame(rbind(c('11061002','11862192','11083069'),

                          c(" ",'1234567','452589')))

df$f1 <-paste0(df$V1,

               ',',

               df$V2,

               ',',

               df$V3)            

df_1 <- as.data.frame(rbind(c('11862192'),

                            c('145')))

names(df_1)[1] <-'f2'

df <- as.data.frame(cbind(df,df_1))

df <-df[,c(4,5)]

The expected output is the third column with values: 11061002,11083069 as 11862192 was present in both. ,1234567,452589 as there is not 145 present in the second column.

Please guide.

1 Answer

0 votes
by (108k points)

What you can do is just split the string on, in f1, and apply the setdiff() to get values that are not present in f2 after removing empty values.

mapply(function(x, y) toString(setdiff(x[x!=' '], y)), 

                      strsplit(df$f1, ','), df$f2)

#[1] "11061002, 11083069" "1234567, 452589" 

If by chance there are multiple comma-separated values in f2, then just split f2 as well.

mapply(function(x, y) toString(setdiff(x[x!=' '], y)), 

                      strsplit(df$f1, ','), strsplit(df$f2, ','))

If you want to know more about R then do check out the R programming tutorial

Browse Categories

...