r - Merge on two criteria, one is a list -
I have two data frames that look something like this Let's first call data frame "master"
line ID color 1 1C ("blue", "green") 2 1 red 3 2 red 4 3c ("pink", "blue second data frame" detail "call" line ID color 1 year 1 blue 2004 2 1 red 2000 3 1 beat 2005 4 2 red 2005 5 3 pink 1 999 6 3 brown 2008 7 3 blue 1997 8 3 pink 2007 I would like to add a column to the master who That year's values mean When two criteria are met then in detail: - ID matching (it is easy)
- When the $ $ color details are found in the Master $ color ( It has been proven difficult).
I have found that the command ...
which (extension $ color == master $ color) ... will recognize color patterns, but neither have applied to merge or apply an application.
The result should look like this .
line ID White color Mean_Year 1 1c ("blue", "green") 2004.5 2 1 red 2000 3 2 red 2005 4 3c ("pink", "blue", "purple") 2001 P> There are 10,000 rows in the master in my real data and there are 8, 000, 000 lines in the description, if it makes a difference.
I'm not sure that your data input file for the data frame looks like 'master', but Suppose it contains a numerical vector (id) and a character vector (color), through which each or more color in each cell vector of the character is different from the comma, if you import it in R, Get a data frame that looks like this:
line ED color 1 1 "blue, green" 2 1 "red" 3 2 "red" 4 3 "pink, blue, purple" I know this data is for the frame master Different from what is shown above. Anyway, assuming that your input is similar to what I have presented, once imported, the first thing to get rid of white space in the vector master $ color is
master $ Color & lt; Once done ("", "", Master $ color) Once this is done, what you want to do is relatively simple:
ID.empty = for NULL (i in 1: nero (master) {ID.empty [i] = mean (description $ year [details $ id% master $ id [i] & is.element Expansion $ Colors, Unlisted (C (StraSplit (Master $ color [i], ',')))))) Print (id.mp) [1] 2004.5 2000.0 2005.0 2001.0
Comments
Post a Comment