0
votes

I have two data frames that I would like to merge by protein accession names.

df1 is a data frame containing protein accession names correlated to a gene (and there are several of these names). df1 thus contain a "list" of these names separated by semicolons in string format with unique values that never occur again in df1. I have written these names as "A1, B1, ..." below:

df1:

Name                a.value
A1;B1;C1            ...
A2                  ...
A3;B3               ...
A4;B4;C4;D4;E4;F4   ...

df2 is a data frame containing only one of these accession named per row:

df2:

Name  b.value
A2    ...
B3    ...
B4    ...

Both df1 and df2 contain other columns.

I would like the merged data frame be merged so that rows are matched if the accession name in df2 exists as one of the names df1 as follows:

A2   A2                 a.value  b.value
B3   A3;B3              ...      ...
B4   A4;B4;C4;D4;E4;F4  ...      ...

And of course, other columns from both data frames are included.

Any suggestions are greatly appreciated, and let me know if you need me to elaborate on something.

Thanks!

1

1 Answers

1
votes

This gives the requested output:

l <- strsplit(as.character(df1$Name), ';')
df1new <- data.frame(Name = unlist(l), Name.string = rep(df1$Name, lengths(l)))
merge(df2, df1new, by = 'Name', all.x = TRUE)

The result:

   Name       Name.string
1:   A2                A2
2:   B3             A3;B3
3:   B4 A4;B4;C4;D4;E4;F4