How to create column based on subsets from another dataframe with differing number of rows
<p>I am trying to create a new column in a dataframe based on subsets from a different dataframe.</p>

<pre><code>df&lt;-data.frame(Name=c('Team 1','Team 1','Team 1', 'Team 2', 'Team 3','Team 3', 'Team 3','Team 4','Team 4'), In=c(8,25,2,2,1,3,3,9,24), Out=c(40,40,20,3,20,1,1,100,1), Group=c(1,1,1,-1,NA,NA,NA,1,1))

df1&lt;-data.frame(Name=c('Team 1','Team 1','Team 1', 'Team 2','Team 2','Team 2','Team 2', 'Team 3','Team 3', 'Team 3','Team 4','Team 4'), In=c(8,25,2,2,1,3,3,9,24,35,14,19), Out=c(40,40,20,3,20,1,1,1,18,29,31,11))

df1$Group&lt;-''

a&lt;-subset(df,Group=='-1')
b&lt;-subset(df,Group=='1')

head(df)
    Name In Out Group
1 Team 1  8  40     1
2 Team 1 25  40     1
3 Team 1  2  20     1
4 Team 2  2   3    -1
5 Team 3  1  20    NA
6 Team 3  3   1    NA
7 Team 3  3   1    NA
8 Team 4  9 100     1
9 Team 4 24   1     1

head(df1)
     Name In Out Group
1  Team 1  5   4      
2  Team 1  5   4      
3  Team 1 22   2      
4  Team 2 21  13      
5  Team 2 14  21      
6  Team 2 13  11      
7  Team 2 13  21      
8  Team 3 19  13      
9  Team 3 21  18      
10 Team 3 13  29      
11 Team 4 14  31      
12 Team 4 19  11     
</code></pre>

<p>I found what I thought was my answer <a href="https://stackoverflow.com/questions/5619091/create-column-based-on-another-dataframe">here</a>, and this way doesn't use subsets, but it also doesn't work due to the differing numbering of rows.</p>

<pre><code>df1$Group &lt;- df$Group[match(df$Name,df1$Name)]
Error in `$&lt;-.data.frame`(`*tmp*`, "Group", value = c(1, 1, 1, -1, 1,  : 
replacement has 9 rows, data has 12
</code></pre>

<p>What I want for my outcome is to create a column ('Group') in df1 so that if the "Name" is found in subset 'a', then it receives a '-1', and if the name is found in subset 'b', then it receives a '1', and everything else that does not fit the category is either left blank or 'NA'.</p>

<p>Example of wanted outcome:</p>

<pre><code> head(df1)
     Name In Out Group
1  Team 1  5   4     1
2  Team 1  5   4     1
3  Team 1 22   2     1
4  Team 2 21  13    -1
5  Team 2 14  21    -1
6  Team 2 13  11    -1
7  Team 2 13  21    -1
8  Team 3 19  13    NA
9  Team 3 21  18    NA
10 Team 3 13  29    NA
11 Team 4 14  31     1
12 Team 4 19  11     1
</code></pre>

<p>The datasets I am working with are still being added to, and are very large, and so it's nonsensical to do it manually. I've been stuck on this for a while, so hopefully one of you can help me figure this out. Thanks.</p>