Collect with tidyr: position must be between 0 and n error

I have some data as shown below:

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 

Now I can make them long with gather() from the tidyr package by writing:

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

But when I use

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

which is my intuitive choice, I get an error

Error: position must be between 0 and n

Why and how can this be solved?

+7
source share
2 answers
 x.row10 %>% gather(Year, Val, c(2:8)) 
+3
source

The question is marked as resolved, but I think it might be useful to post my answer. What David Arenburg does is right. You need exact quotes for this to work. If you use quotes like @uncool in the comments, you will get the same error as it:

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

For German keyboard users: If you don’t know how to type backtick (like I did a few minutes ago):

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

+5
source

All Articles