2016-03-15 64 views
2

試圖將結果列從df2分離爲2個單獨的列(「Winner」,「Loser」),並使用下面的代碼從每個新列中刪除數字,但會收到以下錯誤消息。進行糾正?dplyr單獨返回錯誤

df2 <- data.frame(Year = c(2015:1903), Results = c("Winner", "Loser")) 
df2 %>% separate(type, c("Winner", "Loser"), ",") 

Error in if (!after) c(values, x) else if (after >= lengx) c(x, values) else c(x[1L:after], : 
argument is of length zero 

DF2

Year        Results     MVP 
1 2015       Royals 4, Mets 1  Salvador Perez 
2 2014      Giants 4, Royals 3 Madison Bumgarner 
3 2013     Red Sox 4, Cardinals 2   David Ortiz 
4 2012      Giants 4, Tigers 0  Pablo Sandoval 
5 2011     Cardinals 4, Rangers 3  David Freese 
. 
. 
125 1906  Chicago White Sox 4, Chicago Cubs 2     -- 
126 1905   NY Giants 4, Philadelphia A's 1     -- 
128 1903   Boston Red Sox 5, Pittsburgh 3     -- 
+1

你已經檢查了'DF2 <做到這一點 - 數據。框架(...'這是給我錯誤 – akrun

+0

你也可以使用'separate_' –

回答

5

這裏是separate()一個解決方案。在您的代碼,你有separate()type。您可能要仔細檢查。在這裏,我創建了一個示例數據幀calle d df2並做了以下工作。首先,我刪除了Results中的空格和數字。然後,我使用separate()分隔了列。

library(dplyr) 
library(tidyr) 

mutate(df2, Results = gsub(pattern = "\\s|\\d+", replacement = "", x = Results)) %>% 
separate(col = "Results", into = c("Winner", "Loser"), sep = ",") 

# Year Winner  Loser 
#1 2000 Royals  Mets 
#2 2001 Giants Royals 
#3 2002 RedSox Cardinals 
#4 2003 Giants Tigers 
#5 2004 Cardinals Rangers 

DATA

df2 <- structure(list(Year = 2000:2004, Results = c("Royals 4, Mets 1", 
"Giants 4, Royals 3", "Red Sox 4, Cardinals 2", "Giants 4, Tigers 0", 
"Cardinals 4, Rangers 3")), .Names = c("Year", "Results"), row.names = c(NA, 
-5L), class = "data.frame") 

# Year    Results 
#1 2000  Royals 4, Mets 1 
#2 2001  Giants 4, Royals 3 
#3 2002 Red Sox 4, Cardinals 2 
#4 2003  Giants 4, Tigers 0 
#5 2004 Cardinals 4, Rangers 3 
+0

謝謝你的解決方案 – danielhong

+0

@da尼爾紅樂趣。 :) – jazzurro

+0

如果你不介意,後續的問題,當我試圖總結勝利者和輸家,我不能這樣做,我需要創建一個新的data.frame? – danielhong

3

您也可以使用tidyr::extract(使用@jazzurros數據)單個呼叫

extract(df2, 
     "Results", c("Winner", "Loser"), 
     "([[:alpha:] ]+)\\s+\\d+,\\s+([[:alpha:] ]+)\\s+\\d+") 
# Year Winner  Loser 
# 1 2000 Royals  Mets 
# 2 2001 Giants Royals 
# 3 2002 Red Sox Cardinals 
# 4 2003 Giants Tigers 
# 5 2004 Cardinals Rangers 
+0

謝謝你的解決方案 – danielhong