2017-06-09 31 views
1
         Province     ElecDistName        Candidate Votes Majority Vper MajPer 
              <chr>       <chr>         <chr> <int> <int> <dbl> <dbl> 
1 Newfoundland and Labrador/Terre-Neuve-et-Labrador St. John's East/St. John's-Est      Nick Whalen Liberal 20974  646 46.7 1.4 
2 Newfoundland and Labrador/Terre-Neuve-et-Labrador St. John's East/St. John's-Est Jack Harris ** NDP-New Democratic Party 20328  NA 45.3  NA 
3 Newfoundland and Labrador/Terre-Neuve-et-Labrador St. John's East/St. John's-Est   Deanne Stapleton Conservative 2938  NA 6.5  NA 
4 Newfoundland and Labrador/Terre-Neuve-et-Labrador St. John's East/St. John's-Est  David Anthony Peters Green Party 500  NA 1.1  NA 
5 Newfoundland and Labrador/Terre-Neuve-et-Labrador St. John's East/St. John's-Est     Sean Burton Communist 140  NA 0.3  NA 
6     New Brunswick/Nouveau-Brunswick     Fundy Royal     Alaina Lockhart Liberal 19136  1775 40.9 3.8 

Top of Dataset通过拆分模式的字符列在这里匹配

业余的问题,我想在候选列一分为二,一个包含名称,另含的一方。我尝试了一些在这里发布的单独功能:

separate(ElecResults, Candidate, into = c("Name", "Party"), sep = " (?=[^ ]+$)") 

但是,这似乎错过了很多意见。对于有三个名字的候选人来说,这个问题是显而易见的,但也有一些人似乎完全错过了(对于一个人来说,这个候选人带着莫名其妙的双星号)。

我试图想到如果函数与grepl结合在一起,它会识别自由派,保守派,NDP和绿色等最常见的派对名称,并创建一个名为派对的新列,每次尝试时不断收到错误消息。

如果有人对我如何拆分这个专栏有一个想法,这将是一个巨大的帮助。

谢谢!

下面是使用dput代码:

structure(list(Province = c("Newfoundland and Labrador/Terre-Neuve-et-Labrador", 
"Newfoundland and Labrador/Terre-Neuve-et-Labrador", "Newfoundland and Labrador/Terre-Neuve-et-Labrador", 
"Newfoundland and Labrador/Terre-Neuve-et-Labrador", "Newfoundland and Labrador/Terre-Neuve-et-Labrador", 
"New Brunswick/Nouveau-Brunswick"), ElecDistName = c("St. John's East/St. John's-Est", 
"St. John's East/St. John's-Est", "St. John's East/St. John's-Est", 
"St. John's East/St. John's-Est", "St. John's East/St. John's-Est", 
"Fundy Royal"), Candidate = c("Nick Whalen Liberal", "Jack Harris ** NDP-New Democratic Party", 
"Deanne Stapleton Conservative", "David Anthony Peters Green Party", 
"Sean Burton Communist", "Alaina Lockhart Liberal"), Votes = c(20974L, 
20328L, 2938L, 500L, 140L, 19136L), Majority = c(646L, NA, NA, 
NA, NA, 1775L), Vper = c(46.7, 45.3, 6.5, 1.1, 0.3, 40.9), MajPer = c(1.4, 
NA, NA, NA, NA, 3.8)), .Names = c("Province", "ElecDistName", 
"Candidate", "Votes", "Majority", "Vper", "MajPer"), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame")) 
+0

难以帮助,而无需查看数据对象的外观。 – ssp3nc3r

+0

对不起,我已经发布了代码 – lazslo93

+0

你有多少个不同的派对?并且它们是否统一命名(例如,保守性总是保守的) – Val

回答

0

下面是一个使用fuzzyjoin


library(tidyverse) 
library(fuzzyjoin) 

parties <- data_frame(party = c("Liberal", "NDP-New Democratic Party", "Conservative", "Green Party", "Communist")) 

df %>% 
    regex_left_join(parties, by = c(Candidate = "party")) %>% 
    replace_na(list(party = "minor")) %>% 
    mutate(Candidate = str_replace(Candidate, party, "")) %>% 
    select(Candidate, party) 
#> # A tibble: 6 x 2 
#>    Candidate     party 
#>     <chr>     <chr> 
#> 1   Nick Whalen     Liberal 
#> 2  Jack Harris ** NDP-New Democratic Party 
#> 3  Deanne Stapleton    Conservative 
#> 4 David Anthony Peters    Green Party 
#> 5   Sean Burton     Communist 
#> 6  Alaina Lockhart     Liberal 

注意,最后选择只加到表明,该方法适用的另一种方法。我特别喜欢这种方法,因为可能在数据框中显示的其他方可以很好地处理使用replace_na

+0

谢谢,这工作得很好! – lazslo93

0

下面是一些基本的代码,你需要国防部。将每个聚会名称放在由|分隔的引号内

require(dplyr) 
require(stringr) 

df <- data.frame(Candidate = "Nick Whalen Liberal", Majority = 1) 
parties <- c("Liberal|Conservative") 
df %>% mutate(Name = str_sub(Candidate, 1, str_locate(Candidate, parties)[1] - 1)) 
+0

谢谢!我会玩这个 – lazslo93