Convert columns to multiple rows per record in R

I have the following data:

word Jan-2013 Feb-2013 Mar-2013
A 1 2 3 
B 5 2 4

I want to convert multiple date columns to a single named date and add an extra column for the value.

word date value
A Jan-2013 1
A Feb-2013 2
A Mar-2013 3
B Jan-2013 5
B Feb-2013 2
B Mar-2013 4

Can anyone help?

thank

+1
source share
2 answers

Additional options R

In addition to the “Metrics” answer, here are two additional options for R (if yours data.frameis called “mydf”):

cbind(mydf[1], stack(mydf[-1]))

library(reshape)
melt(mydf, id.vars="word")

Excel option

Excel, "Excel", Tableau Reshaper Excel add-on.

:

  • "" .

  • , , . "".

  • .

    Basic "wide" to "long" reshaping with Tableau's Excel add-on

+7

reshape R (df1 - dataframe)

 reshape(df1,times=names(df1)[-1],timevar="date",varying=names(df1)[-1],v.names="value",new.row.names=1:6,ids=NULL,direction="long")
  word     date value
1    A Jan.2013     1
2    B Jan.2013     5
3    A Feb.2013     2
4    B Feb.2013     2
5    A Mar.2013     3
6    B Mar.2013     4
+3

Source: https://habr.com/ru/post/1693920/


All Articles