0
  1. While practicing the gather function with the examples in gather document. I found the result is not automatically named as stock and price I gave in the gather function argument. Thus I'll have to use mutate or rename function after that. Is that per design or how can I get the name there with gather function? I also checked the question previously asked, tidyr gather: simultaneously gather and rename key?, however, I tried the answer provided by Steven, but it will show:

    Error in match.names(clabs, names(xi)) :
    names do not match previous names" when I do gather.

  2. For rename function, do we have to specify every name even though I only want to rename certain columns? It shows

    Error: All arguments to rename must be named.

So looks like I need to assign to the same name itself if I don't want to change some column name?

I am using Mac with Rstudio v0.98.1103, tidyr v0.20, dplyr v0.41

library(dplyr)
# From https://stackoverflow.com/questions/1181060
stocks <- data.frame(
    time = as.Date('2009-01-01') + 0:9,
    X = rnorm(10, 0, 1),
    Y = rnorm(10, 0, 2),
    Z = rnorm(10, 0, 4)
)

gather(stocks, stock, price, -time)

similar questions in another thread,Can't change the column names outputted by "gather" to be anything other than the default names

I tried to detach plyr but still not working.

stock_long1 result

stocks result

Community
  • 1
  • 1
kuo shu
  • 15
  • 4
  • http://stackoverflow.com/questions/32598113/why-doesnt-gather-use-the-key-variable-name?rq=1 Update the package with update.packages() can solve thi. – kuo shu Oct 08 '15 at 00:12

1 Answers1

0

I do get the right result of stock and price names with

library(tidyr) # You had put dplyr
library(dplyr) # For the pipes %>%

# From http://stackoverflow.com/questions/1181060
stocks <- data.frame(
  time = as.Date('2009-01-01') + 0:9,
  X = rnorm(10, 0, 1),
  Y = rnorm(10, 0, 2),
  Z = rnorm(10, 0, 4)
)

stocks_long1 <- stocks %>%
  gather(stock, price, -time)

stocks_long1

If what you want to rename is X, Y and/or Z, then you can do

# Rename before gathering
stocks_long2 <- stocks %>%
  rename(X1=X, Y1=Y, Z1=Z) %>%
  gather(stock, price, -time)

stocks_long2

Hope this helps

EDIT:

If you don't want the time variable, you can do something like

stocks_long3 <- stocks %>%
  gather(stock, price, -time) %>%
  select(-time)

stocks_long3
Felipe Gerard
  • 1,552
  • 13
  • 23
  • I run the code you provided with stocks_long1 but the name is still variable and value, the time is not removed. I am confused what makes the difference. > stocks_long1 time variable value 1 2009-01-01 X -0.5325 2 2009-01-02 X -0.2173 – kuo shu Oct 07 '15 at 23:24
  • Try putting `tidyr::gather` instead of just `gather`. I got the right result, so maybe you are having some problems with masking the function or something like that. – Felipe Gerard Oct 07 '15 at 23:33
  • I tried with tidyr::gather but still got the same result :(, Thanks for your advice, so looks like I can try to find some resource about the masking to solve this? Really appreciate for your answer. – kuo shu Oct 07 '15 at 23:41
  • No problem. Could you edit your question and add the result you get when you run `stocks_long1` (and also `stocks`)? Do you get any errors/warnings? Something _really_ weird is going on. – Felipe Gerard Oct 07 '15 at 23:43
  • looks like it's the package version issue, it's fixed with later tidyr version. http://stackoverflow.com/questions/32598113/why-doesnt-gather-use-the-key-variable-name?rq=1 really thanks for you help. – kuo shu Oct 08 '15 at 00:10