PavoDive
PavoDive

Reputation: 6496

How do I keep strings in first column with tidyr::gather?

This may be a very basic question about tidyr, which I just started learning, but I don't seem to find an answer after much searching in SO and Google.

Suppose I have a data frame:

mydf<- data.frame(name=c("Joe","Mary","Bob"),
                  jan=1:3,
                  feb=4:6,
                  mar=7:9,
                  apr=10:12)

which I want to reshape from wide to long. Before, I used melt, so:

library(reshape)
melt(mydf,id.vars = "name",measure.vars = colnames(mydf)[-1])

Which produces

   name variable value
1   Joe      jan     1
2  Mary      jan     2
3   Bob      jan     3
4   Joe      feb     4
5  Mary      feb     5
6   Bob      feb     6
7   Joe      mar     7
8  Mary      mar     8
9   Bob      mar     9
10  Joe      apr    10
11 Mary      apr    11
12  Bob      apr    12

I wanted to use tidyr::gather, so I tried

gather(mydf,month,sales,jan:apr)

Which produces

   name month sales
1     2   jan     1
2     3   jan     2
3     1   jan     3
4     2   feb     4
5     3   feb     5
6     1   feb     6
7     2   mar     7
8     3   mar     8
9     1   mar     9
10    2   apr    10
11    3   apr    11
12    1   apr    12

I'm lost here, as I haven't been able to keep the names in the first column.

What am I missing here?

######### EDIT TO ADD #######

> R.Version()$version.string
[1] "R version 3.2.2 (2015-08-14)"
> packageVersion("tidyr")
[1] ‘0.3.0’

Upvotes: 3

Views: 232

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

It looks like in tidyr 0.3.0 you will need to convert the factor column name to character. I'm not sure why that has changed from version 0.2.0, where it worked without conversion to character. Nevertheless, here we go ...

gather(transform(mydf, name = as.character(name)), month, sales, jan:apr)
#    name month sales
# 1   Joe   jan     1
# 2  Mary   jan     2
# 3   Bob   jan     3
# 4   Joe   feb     4
# 5  Mary   feb     5
# 6   Bob   feb     6
# 7   Joe   mar     7
# 8  Mary   mar     8
# 9   Bob   mar     9
# 10  Joe   apr    10
# 11 Mary   apr    11
# 12  Bob   apr    12

R.version.string
# [1] "R version 3.2.2 (2015-08-14)"
packageVersion("tidyr")
# [1] ‘0.3.0’

Credit to @aosmith for finding the closed github issue. You should be able to use the development version without issue now. To install the dev version, use

devtools::install_github(
    "hadley/tidyr", 
    ref = "2e08772d154babcc97912bcae8b0b64b65b964ab"
)

Upvotes: 2

Related Questions