0

I have a data frame that has 7 rows and 156 variables. One of the columns is the Date column and the other columns are names of countries that have total number of installs for each of the seven days. It looks something like this:

Dates      USA  Argentina  France...
06/01/2020  3       6         1
06/02/2020  2       4         6 
.           .       .         .
.           .       .         .

I wanted to use melt() from reshape2 to get it into long format so I typed the following code

dfnames<-names(df)[-1] #I don't want the variable Dates to be included
df<-melt(df, id.vars = dfnames, variable.name = "country", value.name ="installs")

instead of getting something that looked like this

Dates       country  installs
06/01/2020  USA         3
06/02/2020  USA         2
.
.
06/01/2020  Argentina   6
06/01/2020  Argentina   4
.
.
etc

I get the same data frame where nothing has changed except that the Dates column is gone and that there are the columns country and installs added on with country having 7 entries of the word Dates and installs the actual dates.

Nick
  • 369
  • 1
  • 3
  • 18
  • 2
    You are misunderstanding the `id.vars` parameter: `df<-melt(df, id.vars = "Dates", variable.name = "country", value.name ="installs")` – Roland Jun 18 '20 at 14:19
  • `id.vars` should be some column (or columns) that is preserved - usually an ID column. In this case `date` is the only thing that fits the bill. And... you show `Dates` in your desired result, so I'm confused by the statement *"I don't want the variable Dates to be included"* – Gregor Thomas Jun 18 '20 at 14:20
  • I'd suggest running through the examples at the bottom of the `?melt` help page, or looking at the answers to the FAQ about [Reshaping data from wide to long](https://stackoverflow.com/q/2185252/903061). – Gregor Thomas Jun 18 '20 at 14:21
  • @Gregor Thomas I made that statement thinking that id.vars needed all the names of the columns that would become entries of the new variable country and so I didn't want Dates to become entries of country. – Nick Jun 22 '20 at 07:42

0 Answers0