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

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

I have some data like 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 the error message

Error: Position must be between 0 and n

How come and how can this be resolved?

回答1:

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


回答2:

The question is marked as solved, but i think it maybe usefull to post my answer. The way David Arenburg does is right. You need exakt backsticks for it to work. If you use the Quotation marks as uncool in comment did, you get the same error like him:

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

For german keybord user: If you dont know (like me few minutes ago:-) ) how to type backstick:



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!