I assume this is rather straightforward, but unfortunately I haven't been able to figure out the solution. I found hints here and here, but they weren't solving the issue.
I have a dataframe with election results (x.melt; several thousand rows) and another dataframe (parties.bih) which contains only the party names and the ethnic affiliations. The two dataframes have different dimensions.
dataframe parties.bih
party ethnicity
1 BPS B
2 SBiH B
3 SDP X
4 SDS S
5 DNZ B
6 SDA B
7 PDP S
8 DNS S
9 NSRzB C
10 SNSD S
11 HDZ.1990.HZ.HSS.HKDU.HDU. C
12 HDZ.HNZ C
13 SBBBiH B
14 HDZBiH C
15 Croatian.Coalition C
All I want to do is to add the ethnic affiliation of each party (in dataframe parties.bih) to the election results (in dataframe x.melt).
When I try this command, the ethnic affiliations are added, but not in accordance with each party.
x.melt$ethnicity[x.melt$party==parties.bih$party] <- parties.bih$ethnicity
When I try the merge command, the row "party" disappears. I could of course duplicate the party column, but there must be a more straightforward solution.
tt <- merge(x.melt, parties.bih, by.x="party", by.y="party", all.x=TRUE)
I am still rather new to R, and assume that this is quite easy actually, but I have simply not figured it out. Many thanks.