2017-02-22 5 views
3

私は2016年の主要選挙結果を持つデータセットを持っています。データセットにはState、state_abbr、county、fips(州と郡のID番号の組み合わせ)、党、候補、票、小数投票の8つの列があります。それぞれの候補者の各郡で「勝ち」または「喪失」を示す「結果」という新しい列を作成したいと考えています。勝者は常に投票の50%を取得していないので、rのデータフレーム内の既存の変数を比較して新しい変数を追加する

Democrat$result <- ifelse(Democrat$fraction_votes > .5, "Win","Loss") 

これは、明らかに正確な方法ではありません:私は列を追加し、このコードを使用し、その後、2人の民主的な候補者にdplyrを使用してデータを濾過しました。 Rに各郡のvote_fractionまたは投票総額を比較させ、「勝利」または「損失」を返すにはどうすればよいですか? apply()ファミリ、forループ、関数の作成は、新しい列を作成する最良の方法でしょうか?

state state_abbreviation county fips party  candidate 
    1 Alabama AL   Autauga 1001 Democrat Bernie Sanders 
    2 Alabama AL   Autauga 1001 Democrat Hillary Clinton 
    3 Alabama AL   Baldwin 1003 Democrat Bernie Sanders 
    4 Alabama AL   Baldwin 1003 Democrat Hillary Clinton 
    5 Alabama AL   Barbour 1005 Democrat Bernie Sanders 
    6 Alabama AL   Barbour 1005 Democrat Hillary Clinton 
    votes fraction_votes 
    1 544   0.182 
    2 2387   0.800 
    3 2694   0.329 
    4 5290   0.647 
    5 222   0.078 
    6 2567   0.906 
+1

を返し

library(data.table) # convert to data.table setDT(Democrat) # get logical vector that proclaims winner if vote fraction is maximum Democrat[, winner := fraction_votes == max(fraction_votes), by=fips] 

は、我々はあなたのデータセットの例を得ることができますか? –

+0

あなたの投稿! –

+0

Ok、そこには –

答えて

1

Iは、まず、結果を計算し、元のデータセットに郡最大の列を追加し、任意の候補が所定の郡で受信した投票の最大数を見つけるためにdplyrパッケージからsummarise関数を使用します。

# create a sample dataset akin to the question setup 
df <- data.frame(abrev = rep("AL", 6), county = c("Autuga", "Autuga", "Baldwin", "Baldwin", 
                "Barbour", "Barbour"), 
       party = rep("Democrat", 6), 
       candidate = rep(c("Bernie", "Hillary"), 3), 
       fraction_votes = c(0.18, 0.8, 0.32, 0.64, 0.07, 0.9)) 

# load a dplyr library 
library(dplyr) 

# calculate what was the maximum ammount of votes candidate received in a given county 

# take a df dataset 
winners <- df %>% 
     # group it by a county 
     group_by(county) %>% 
     # for each county, calculate maximum of votes 
     summarise(score = max(fraction_votes)) 

# join the original dataset and the dataset with county maximumus 
# join them by county column 
df <- left_join(df, winners, by = c("county")) 

# calculate the result column 
df$result <- ifelse(df$fraction_votes == df$score, "Win", "Loss") 

同じ名前を持つ別の郡がある場合は、グループを調整し、部品を接合しなければならないが、ロジックが基地Rは同じ

+0

非常にうまくいった! –

1

なければならない、あなたはave有するバイナリーベクターを算出することができます:

Democrat 
    state state_abbreviation county fips party candidate votes fraction_votes winner 
1 Alabama     AL Autauga 1001 Democrat Bernie 544   0.182  0 
2 Alabama     AL Autauga 1001 Democrat Hillary 2387   0.800  1 
3 Alabama     AL Baldwin 1003 Democrat Bernie 2694   0.329  0 
4 Alabama     AL Baldwin 1003 Democrat Hillary 5290   0.647  1 
5 Alabama     AL Barbour 1005 Democrat Bernie 222   0.078  0 
6 Alabama     AL Barbour 1005 Democrat Hillary 2567   0.906  1 

返し

Democrat$winner <- ave(Democrat$fraction_votes, Democrat$fips, FUN=function(i) i == max(i)) 

必要であればas.logicalaveをラップして論理に変換することができます。


これはdata.tableでもかなり簡単です。 FIPSは、ユニークな状態-郡のIDであると仮定すると:

Democrat 
    state state_abbreviation county fips party candidate votes fraction_votes winner 
1: Alabama     AL Autauga 1001 Democrat Bernie 544   0.182 FALSE 
2: Alabama     AL Autauga 1001 Democrat Hillary 2387   0.800 TRUE 
3: Alabama     AL Baldwin 1003 Democrat Bernie 2694   0.329 FALSE 
4: Alabama     AL Baldwin 1003 Democrat Hillary 5290   0.647 TRUE 
5: Alabama     AL Barbour 1005 Democrat Bernie 222   0.078 FALSE 
6: Alabama     AL Barbour 1005 Democrat Hillary 2567   0.906 TRUE 

データ

Democrat <- 
structure(list(state = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Alabama", class = "factor"), 
    state_abbreviation = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "AL", class = "factor"), 
    county = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("Autauga", 
    "Baldwin", "Barbour"), class = "factor"), fips = c(1001L, 
    1001L, 1003L, 1003L, 1005L, 1005L), party = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "Democrat", class = "factor"), 
    candidate = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Bernie", 
    "Hillary"), class = "factor"), votes = c(544L, 2387L, 2694L, 
    5290L, 222L, 2567L), fraction_votes = c(0.182, 0.8, 0.329, 
    0.647, 0.078, 0.906)), .Names = c("state", "state_abbreviation", 
"county", "fips", "party", "candidate", "votes", "fraction_votes" 
), row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame") 
関連する問題