2015-09-10 35 views
5

之間我有一些數據,如下面:收集與tidyr:位置必須在0到n錯誤

x.row10 %>% gather(Year, Val, -names) 

x.row10 <- setNames(data.frame(letters[1:3],1:3,2:4,3:5,4:6,5:7,6:8,7:9), 
        c("names",2004:2009,2012)) 
# names 2004 2005 2006 2007 2008 2009 2012 
#1  a 1 2 3 4 5 6 7 
#2  b 2 3 4 5 6 7 8 
#3  c 3 4 5 6 7 8 9 

現在,我可以寫作讓他們長着gather()tidyr

但是當我用

x.row10 %>% gather(Year, Val, c(2004:2009,2012)) 

這是我的直覺選擇,我得到錯誤m essage

Error: Position must be between 0 and n

怎麼回事,怎麼能解決這個問題?

+4

添加反引號,如'''DF%>%聚集(年,纈氨酸,C('2004':'2009','2012'))'''或使用有效列名。 'gather'認爲你正試圖選擇超出範圍的列索引。 –

+0

我收到一條錯誤消息:錯誤:所有select()輸入都必須解析爲整數列位置。 以下內容不適用: * c(「2004」:「2009」,「2012」)使用反引號時 – uncool

+2

適用於我。 'df'應該是'x.row10' ofcourse –

回答

4
x.row10 %>% gather(Year, Val, c(2:8)) 
+2

不是'x.row10'或'df',不是兩者。 –

+1

@StevenBeaupré好點,謝謝 – HubertL

3

該問題被標記爲已解決,但我認爲這可能是有用的發佈我的答案。大衛阿倫堡的做法是對的。你需要exakt backsticks它的工作。如果在評論使用引號爲酷沒有,你得到同樣的錯誤他一樣:

Error: All select() inputs must resolve to integer column positions. 
The following do not: 
* c("2004":"2009", "2012") 

德國keybord用戶:如果你不知道(比如我在幾分鐘前:-))如何類型backstick:

"Shift + the key on the right side of ß" and after that "spacebar".

相關問題