r - Creating a column with differences based on another column -
I have a data frame that looks like this (simplified from 699 treaties):
< Code> TRT & lt; - data.frame (T.ID = c (1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,8), treaty = c ( "Hungary Slovenia 1994", "Hungary, Slovenia 1994", "Nicaragua Taiwan 2006", "Nicaragua Taiwan 2006", "Ukraine Uzbekistan 1994", "Ukraine Uzbekistan 1994", "Brazil, Uruguay 1986," "Brazil, Uruguay in 1986", "Albania Macedonia 2002 "," Albania, Macedonia 2002 "," Albania, Moldova 2003 "," Albania, Moldova 2003 "," Albania, Romania 2003 "," Albania, Romania 2003 "," Izmir 1977 Treaty "," Izmir 1977 Treaty "," Izmir 1 9 77 of the Treaty "), CK = C (" Hun 1 9 4 "," SLV 1 9 4 "," NIC 2006 "," Tiaa 2006 "," Ukear 1 9 4 9 " "Uzbl994", "Brl986", "Urul986", "Alb2002", "Ma2002", "Alb2003", "Mald2003", "Alb2003", "Rum2003", "Irnl977", "Turl977", "Pakl977"), Prom .demo = c (1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0), Politics = C (10,10,8, 10,7, -9,7,9,7,9,7,8,7,8, -10, -7))
end In, I want each treaty only to list each criterion of maximum and minimum difference between its value column and "Constitution" in each column only once, for each "treaty" Contract parties of each treaty (most treaties have only two contracting parties, but 51 of some) are there any R orders that I calculate spares 699?
After
using dplyr
your scood
and After joining the year
, the group is grouped by agreement
and then the minimum and maximum polity
:
require (dplyr) left_join (treaties, politics, c ( "scode", "year"))% & gt;% group_by (Treaty)% & gt;% short (PolityDiff = max (politics, na.rm = TRUE) Cleanliness MIN (politics, na.rm = TRUE) Source: Local data frame [8x2] Treaty PolityDiff 1 Macedonia Albania 2 002 2 2 Moldova Albania 2003 1 3 Romania 2003 1 4 Brazil Albania Uruguay 1986 2 5 Hungary Slovenia 1994 0 6 Nicaragua Taiwan 2006 2 7 Izmir Treaty of 1977 NA 8 Ukraine Uzbekistan 1994 16
NA is where you nobody is / strong> Milan region / year (Izmir's treaty is in IRN / Tour / Pakistan, 1977, and none of them are included in policy figures.)
Note that if you want NA if If the policy is not in the any policy, then use it:
left_join (treaties, politics, c ("scode", "year" ))% & Gt;% group_by (treaty)% & gt;% abbreviation (PolityDiff = Maximum) Cleanliness MIN (politics))
which gives:
< Pre> treaty PolityDiff 1 Albania Macedonia 2002 2 2 Albania Moldova 2003 1 3 Albania Romania 2003 1 4 Brazil Uruguay 1986 2 5 Brown Slovenia 1994 NA 6 Nicaragua Taiwan 2006 Izmir 2 to 7 Treaty 1977 NA 8 Ukraine Right Bekistan 1994 16
Because Slovenia is coded as SLV in politics but not in SLO treaties - mistake? Anyway, there is no SLO / 1994 policy so that the treaty can be returned in the form of NA in this manner. This is my first example of zero returns because NA is dropped and the difference between the state system is a number and the difference between the two ...
Comments
Post a Comment