How to stack a dataframe in R [duplicate]









up vote
2
down vote

favorite













This question already has an answer here:



  • Reshaping data.frame from wide to long format

    5 answers



I have a data frame that I would like to stack in R so that I end up with three columns. Below cis some example data in its current format.



> dput(df)
structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
"d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L), A1.1 = c(34L,
55L, 75L, 12L, 56L, 35L, 3L, 22L, NA, NA)), .Names = c("Day",
"A1", "A2", "A3", "A1.1"), class = "data.frame", row.names = c(NA,
-10L))

> df
Day A1 A2 A3 A1.1
1 d1 14 9 3 34
2 d2 24 15 4 55
3 d3 22 34 19 75
4 d4 NA 2 76 12
5 d5 NA 12 34 56
6 d6 NA 34 34 35
7 d7 NA 234 23 3
8 d8 NA 34 24 22
9 d9 NA NA 37 NA
10 d10 NA NA 44 NA


I would like to end up with the dataframe formatted like so with three columns entitles "Day", "Animal" and "Count":



 Day Animal Count
d1 A1 14
d2 A1 24
d3 A1 22
d4 A1 NA
d5 A1 NA
d6 A1 NA
d7 A1 NA
d8 A1 NA
d9 A1 NA
d10 A1 NA
d1 A2 9
d2 A2 15
d3 A2 34
d4 A2 2
d5 A2 12
d6 A2 34
d7 A2 234
d8 A2 34
d9 A2 NA
d10 A2 NA
d1 A3 3
d2 A3 4
d3 A3 19
d4 A3 76
d5 A3 34
d6 A3 34
d7 A3 23
d8 A3 24
d9 A3 37
d10 A3 44
d1 A1 34
d2 A1 55
d3 A1 75
d4 A1 12
d5 A1 56
d6 A1 35
d7 A1 3
d8 A1 22
d9 A1 NA
d10 A1 NA


I know that this should be an easy task but I am really struggling to find the solution. Any help with be much appreciated.










share|improve this question















marked as duplicate by zx8754 r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















    up vote
    2
    down vote

    favorite













    This question already has an answer here:



    • Reshaping data.frame from wide to long format

      5 answers



    I have a data frame that I would like to stack in R so that I end up with three columns. Below cis some example data in its current format.



    > dput(df)
    structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
    "d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
    NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
    ), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L), A1.1 = c(34L,
    55L, 75L, 12L, 56L, 35L, 3L, 22L, NA, NA)), .Names = c("Day",
    "A1", "A2", "A3", "A1.1"), class = "data.frame", row.names = c(NA,
    -10L))

    > df
    Day A1 A2 A3 A1.1
    1 d1 14 9 3 34
    2 d2 24 15 4 55
    3 d3 22 34 19 75
    4 d4 NA 2 76 12
    5 d5 NA 12 34 56
    6 d6 NA 34 34 35
    7 d7 NA 234 23 3
    8 d8 NA 34 24 22
    9 d9 NA NA 37 NA
    10 d10 NA NA 44 NA


    I would like to end up with the dataframe formatted like so with three columns entitles "Day", "Animal" and "Count":



     Day Animal Count
    d1 A1 14
    d2 A1 24
    d3 A1 22
    d4 A1 NA
    d5 A1 NA
    d6 A1 NA
    d7 A1 NA
    d8 A1 NA
    d9 A1 NA
    d10 A1 NA
    d1 A2 9
    d2 A2 15
    d3 A2 34
    d4 A2 2
    d5 A2 12
    d6 A2 34
    d7 A2 234
    d8 A2 34
    d9 A2 NA
    d10 A2 NA
    d1 A3 3
    d2 A3 4
    d3 A3 19
    d4 A3 76
    d5 A3 34
    d6 A3 34
    d7 A3 23
    d8 A3 24
    d9 A3 37
    d10 A3 44
    d1 A1 34
    d2 A1 55
    d3 A1 75
    d4 A1 12
    d5 A1 56
    d6 A1 35
    d7 A1 3
    d8 A1 22
    d9 A1 NA
    d10 A1 NA


    I know that this should be an easy task but I am really struggling to find the solution. Any help with be much appreciated.










    share|improve this question















    marked as duplicate by zx8754 r
    Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    2 days ago


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite












      This question already has an answer here:



      • Reshaping data.frame from wide to long format

        5 answers



      I have a data frame that I would like to stack in R so that I end up with three columns. Below cis some example data in its current format.



      > dput(df)
      structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
      "d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
      NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
      ), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L), A1.1 = c(34L,
      55L, 75L, 12L, 56L, 35L, 3L, 22L, NA, NA)), .Names = c("Day",
      "A1", "A2", "A3", "A1.1"), class = "data.frame", row.names = c(NA,
      -10L))

      > df
      Day A1 A2 A3 A1.1
      1 d1 14 9 3 34
      2 d2 24 15 4 55
      3 d3 22 34 19 75
      4 d4 NA 2 76 12
      5 d5 NA 12 34 56
      6 d6 NA 34 34 35
      7 d7 NA 234 23 3
      8 d8 NA 34 24 22
      9 d9 NA NA 37 NA
      10 d10 NA NA 44 NA


      I would like to end up with the dataframe formatted like so with three columns entitles "Day", "Animal" and "Count":



       Day Animal Count
      d1 A1 14
      d2 A1 24
      d3 A1 22
      d4 A1 NA
      d5 A1 NA
      d6 A1 NA
      d7 A1 NA
      d8 A1 NA
      d9 A1 NA
      d10 A1 NA
      d1 A2 9
      d2 A2 15
      d3 A2 34
      d4 A2 2
      d5 A2 12
      d6 A2 34
      d7 A2 234
      d8 A2 34
      d9 A2 NA
      d10 A2 NA
      d1 A3 3
      d2 A3 4
      d3 A3 19
      d4 A3 76
      d5 A3 34
      d6 A3 34
      d7 A3 23
      d8 A3 24
      d9 A3 37
      d10 A3 44
      d1 A1 34
      d2 A1 55
      d3 A1 75
      d4 A1 12
      d5 A1 56
      d6 A1 35
      d7 A1 3
      d8 A1 22
      d9 A1 NA
      d10 A1 NA


      I know that this should be an easy task but I am really struggling to find the solution. Any help with be much appreciated.










      share|improve this question
















      This question already has an answer here:



      • Reshaping data.frame from wide to long format

        5 answers



      I have a data frame that I would like to stack in R so that I end up with three columns. Below cis some example data in its current format.



      > dput(df)
      structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
      "d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
      NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
      ), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L), A1.1 = c(34L,
      55L, 75L, 12L, 56L, 35L, 3L, 22L, NA, NA)), .Names = c("Day",
      "A1", "A2", "A3", "A1.1"), class = "data.frame", row.names = c(NA,
      -10L))

      > df
      Day A1 A2 A3 A1.1
      1 d1 14 9 3 34
      2 d2 24 15 4 55
      3 d3 22 34 19 75
      4 d4 NA 2 76 12
      5 d5 NA 12 34 56
      6 d6 NA 34 34 35
      7 d7 NA 234 23 3
      8 d8 NA 34 24 22
      9 d9 NA NA 37 NA
      10 d10 NA NA 44 NA


      I would like to end up with the dataframe formatted like so with three columns entitles "Day", "Animal" and "Count":



       Day Animal Count
      d1 A1 14
      d2 A1 24
      d3 A1 22
      d4 A1 NA
      d5 A1 NA
      d6 A1 NA
      d7 A1 NA
      d8 A1 NA
      d9 A1 NA
      d10 A1 NA
      d1 A2 9
      d2 A2 15
      d3 A2 34
      d4 A2 2
      d5 A2 12
      d6 A2 34
      d7 A2 234
      d8 A2 34
      d9 A2 NA
      d10 A2 NA
      d1 A3 3
      d2 A3 4
      d3 A3 19
      d4 A3 76
      d5 A3 34
      d6 A3 34
      d7 A3 23
      d8 A3 24
      d9 A3 37
      d10 A3 44
      d1 A1 34
      d2 A1 55
      d3 A1 75
      d4 A1 12
      d5 A1 56
      d6 A1 35
      d7 A1 3
      d8 A1 22
      d9 A1 NA
      d10 A1 NA


      I know that this should be an easy task but I am really struggling to find the solution. Any help with be much appreciated.





      This question already has an answer here:



      • Reshaping data.frame from wide to long format

        5 answers







      r dataframe stack reformat






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago

























      asked 2 days ago









      jjulip

      5022919




      5022919




      marked as duplicate by zx8754 r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      2 days ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by zx8754 r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      2 days ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          5
          down vote













          With dplyr and tidyr:



          library(dplyr)
          library(tidyr)
          df <- df %>%
          gather("animal", "count", -Day)


          head(df)
          # Day animal count
          # 1 d1 A1 14
          # 2 d2 A1 24
          # 3 d3 A1 22
          # 4 d4 A1 NA
          # 5 d5 A1 NA
          # 6 d6 A1 NA





          share|improve this answer


















          • 1




            Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
            – jjulip
            2 days ago










          • I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Call rlang::last_error() to see a backtrace
            – jjulip
            2 days ago











          • It is working for me even with the new data, try cleaning your workspace first then retry
            – RLave
            2 days ago










          • No. I've tried multiple times with multiple examples and I get the same error every time :(
            – jjulip
            2 days ago










          • which version of dplyr and tidyr do you have? See packageVersion("dplyr"). Try update to the last version by reinstalling both: devtools::install_github("hadley/dplyr") or install_packages("dplyr").
            – RLave
            2 days ago


















          up vote
          2
          down vote













          It can be done with base R function melt:



          melt(df)

          OUTPUT :

          Day variable value
          1 d1 A1 14
          2 d2 A1 24
          3 d3 A1 22
          4 d4 A1 NA
          5 d5 A1 NA
          6 d6 A1 NA
          7 d7 A1 NA
          8 d8 A1 NA
          9 d9 A1 NA
          10 d10 A1 NA
          11 d1 A2 9
          12 d2 A2 15
          13 d3 A2 34
          14 d4 A2 2
          15 d5 A2 12
          16 d6 A2 34
          17 d7 A2 234
          18 d8 A2 34
          19 d9 A2 NA
          20 d10 A2 NA
          21 d1 A3 3
          22 d2 A3 4
          23 d3 A3 19
          24 d4 A3 76
          25 d5 A3 34
          26 d6 A3 34
          27 d7 A3 23
          28 d8 A3 24
          29 d9 A3 37
          30 d10 A3 44





          share|improve this answer



























            up vote
            2
            down vote













            You want to format your data frame from a wide format to a long format using the melt function of the reshape 2 package also answered here



            library(reshape2)

            df <- structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
            "d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
            NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
            ), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L)), .Names = c("Day",
            "A1", "A2", "A3"), class = "data.frame", row.names = c(NA, -10L
            ))
            long_format <- melt(df)
            colnames(long_format)[2:3] <- c("Animal","Count")





            share|improve this answer








            New contributor




            seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















            • How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
              – jjulip
              2 days ago










            • @jjulip you can try ?melt.data.frame to get more information on the function. See e.g. under Arguments and id.vars.
              – seth-1
              2 days ago

















            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote













            With dplyr and tidyr:



            library(dplyr)
            library(tidyr)
            df <- df %>%
            gather("animal", "count", -Day)


            head(df)
            # Day animal count
            # 1 d1 A1 14
            # 2 d2 A1 24
            # 3 d3 A1 22
            # 4 d4 A1 NA
            # 5 d5 A1 NA
            # 6 d6 A1 NA





            share|improve this answer


















            • 1




              Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
              – jjulip
              2 days ago










            • I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Call rlang::last_error() to see a backtrace
              – jjulip
              2 days ago











            • It is working for me even with the new data, try cleaning your workspace first then retry
              – RLave
              2 days ago










            • No. I've tried multiple times with multiple examples and I get the same error every time :(
              – jjulip
              2 days ago










            • which version of dplyr and tidyr do you have? See packageVersion("dplyr"). Try update to the last version by reinstalling both: devtools::install_github("hadley/dplyr") or install_packages("dplyr").
              – RLave
              2 days ago















            up vote
            5
            down vote













            With dplyr and tidyr:



            library(dplyr)
            library(tidyr)
            df <- df %>%
            gather("animal", "count", -Day)


            head(df)
            # Day animal count
            # 1 d1 A1 14
            # 2 d2 A1 24
            # 3 d3 A1 22
            # 4 d4 A1 NA
            # 5 d5 A1 NA
            # 6 d6 A1 NA





            share|improve this answer


















            • 1




              Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
              – jjulip
              2 days ago










            • I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Call rlang::last_error() to see a backtrace
              – jjulip
              2 days ago











            • It is working for me even with the new data, try cleaning your workspace first then retry
              – RLave
              2 days ago










            • No. I've tried multiple times with multiple examples and I get the same error every time :(
              – jjulip
              2 days ago










            • which version of dplyr and tidyr do you have? See packageVersion("dplyr"). Try update to the last version by reinstalling both: devtools::install_github("hadley/dplyr") or install_packages("dplyr").
              – RLave
              2 days ago













            up vote
            5
            down vote










            up vote
            5
            down vote









            With dplyr and tidyr:



            library(dplyr)
            library(tidyr)
            df <- df %>%
            gather("animal", "count", -Day)


            head(df)
            # Day animal count
            # 1 d1 A1 14
            # 2 d2 A1 24
            # 3 d3 A1 22
            # 4 d4 A1 NA
            # 5 d5 A1 NA
            # 6 d6 A1 NA





            share|improve this answer














            With dplyr and tidyr:



            library(dplyr)
            library(tidyr)
            df <- df %>%
            gather("animal", "count", -Day)


            head(df)
            # Day animal count
            # 1 d1 A1 14
            # 2 d2 A1 24
            # 3 d3 A1 22
            # 4 d4 A1 NA
            # 5 d5 A1 NA
            # 6 d6 A1 NA






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 days ago

























            answered 2 days ago









            RLave

            2,5281820




            2,5281820







            • 1




              Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
              – jjulip
              2 days ago










            • I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Call rlang::last_error() to see a backtrace
              – jjulip
              2 days ago











            • It is working for me even with the new data, try cleaning your workspace first then retry
              – RLave
              2 days ago










            • No. I've tried multiple times with multiple examples and I get the same error every time :(
              – jjulip
              2 days ago










            • which version of dplyr and tidyr do you have? See packageVersion("dplyr"). Try update to the last version by reinstalling both: devtools::install_github("hadley/dplyr") or install_packages("dplyr").
              – RLave
              2 days ago













            • 1




              Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
              – jjulip
              2 days ago










            • I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Call rlang::last_error() to see a backtrace
              – jjulip
              2 days ago











            • It is working for me even with the new data, try cleaning your workspace first then retry
              – RLave
              2 days ago










            • No. I've tried multiple times with multiple examples and I get the same error every time :(
              – jjulip
              2 days ago










            • which version of dplyr and tidyr do you have? See packageVersion("dplyr"). Try update to the last version by reinstalling both: devtools::install_github("hadley/dplyr") or install_packages("dplyr").
              – RLave
              2 days ago








            1




            1




            Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
            – jjulip
            2 days ago




            Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
            – jjulip
            2 days ago












            I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Call rlang::last_error() to see a backtrace
            – jjulip
            2 days ago





            I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Call rlang::last_error() to see a backtrace
            – jjulip
            2 days ago













            It is working for me even with the new data, try cleaning your workspace first then retry
            – RLave
            2 days ago




            It is working for me even with the new data, try cleaning your workspace first then retry
            – RLave
            2 days ago












            No. I've tried multiple times with multiple examples and I get the same error every time :(
            – jjulip
            2 days ago




            No. I've tried multiple times with multiple examples and I get the same error every time :(
            – jjulip
            2 days ago












            which version of dplyr and tidyr do you have? See packageVersion("dplyr"). Try update to the last version by reinstalling both: devtools::install_github("hadley/dplyr") or install_packages("dplyr").
            – RLave
            2 days ago





            which version of dplyr and tidyr do you have? See packageVersion("dplyr"). Try update to the last version by reinstalling both: devtools::install_github("hadley/dplyr") or install_packages("dplyr").
            – RLave
            2 days ago













            up vote
            2
            down vote













            It can be done with base R function melt:



            melt(df)

            OUTPUT :

            Day variable value
            1 d1 A1 14
            2 d2 A1 24
            3 d3 A1 22
            4 d4 A1 NA
            5 d5 A1 NA
            6 d6 A1 NA
            7 d7 A1 NA
            8 d8 A1 NA
            9 d9 A1 NA
            10 d10 A1 NA
            11 d1 A2 9
            12 d2 A2 15
            13 d3 A2 34
            14 d4 A2 2
            15 d5 A2 12
            16 d6 A2 34
            17 d7 A2 234
            18 d8 A2 34
            19 d9 A2 NA
            20 d10 A2 NA
            21 d1 A3 3
            22 d2 A3 4
            23 d3 A3 19
            24 d4 A3 76
            25 d5 A3 34
            26 d6 A3 34
            27 d7 A3 23
            28 d8 A3 24
            29 d9 A3 37
            30 d10 A3 44





            share|improve this answer
























              up vote
              2
              down vote













              It can be done with base R function melt:



              melt(df)

              OUTPUT :

              Day variable value
              1 d1 A1 14
              2 d2 A1 24
              3 d3 A1 22
              4 d4 A1 NA
              5 d5 A1 NA
              6 d6 A1 NA
              7 d7 A1 NA
              8 d8 A1 NA
              9 d9 A1 NA
              10 d10 A1 NA
              11 d1 A2 9
              12 d2 A2 15
              13 d3 A2 34
              14 d4 A2 2
              15 d5 A2 12
              16 d6 A2 34
              17 d7 A2 234
              18 d8 A2 34
              19 d9 A2 NA
              20 d10 A2 NA
              21 d1 A3 3
              22 d2 A3 4
              23 d3 A3 19
              24 d4 A3 76
              25 d5 A3 34
              26 d6 A3 34
              27 d7 A3 23
              28 d8 A3 24
              29 d9 A3 37
              30 d10 A3 44





              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                It can be done with base R function melt:



                melt(df)

                OUTPUT :

                Day variable value
                1 d1 A1 14
                2 d2 A1 24
                3 d3 A1 22
                4 d4 A1 NA
                5 d5 A1 NA
                6 d6 A1 NA
                7 d7 A1 NA
                8 d8 A1 NA
                9 d9 A1 NA
                10 d10 A1 NA
                11 d1 A2 9
                12 d2 A2 15
                13 d3 A2 34
                14 d4 A2 2
                15 d5 A2 12
                16 d6 A2 34
                17 d7 A2 234
                18 d8 A2 34
                19 d9 A2 NA
                20 d10 A2 NA
                21 d1 A3 3
                22 d2 A3 4
                23 d3 A3 19
                24 d4 A3 76
                25 d5 A3 34
                26 d6 A3 34
                27 d7 A3 23
                28 d8 A3 24
                29 d9 A3 37
                30 d10 A3 44





                share|improve this answer












                It can be done with base R function melt:



                melt(df)

                OUTPUT :

                Day variable value
                1 d1 A1 14
                2 d2 A1 24
                3 d3 A1 22
                4 d4 A1 NA
                5 d5 A1 NA
                6 d6 A1 NA
                7 d7 A1 NA
                8 d8 A1 NA
                9 d9 A1 NA
                10 d10 A1 NA
                11 d1 A2 9
                12 d2 A2 15
                13 d3 A2 34
                14 d4 A2 2
                15 d5 A2 12
                16 d6 A2 34
                17 d7 A2 234
                18 d8 A2 34
                19 d9 A2 NA
                20 d10 A2 NA
                21 d1 A3 3
                22 d2 A3 4
                23 d3 A3 19
                24 d4 A3 76
                25 d5 A3 34
                26 d6 A3 34
                27 d7 A3 23
                28 d8 A3 24
                29 d9 A3 37
                30 d10 A3 44






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                sai saran

                55318




                55318




















                    up vote
                    2
                    down vote













                    You want to format your data frame from a wide format to a long format using the melt function of the reshape 2 package also answered here



                    library(reshape2)

                    df <- structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
                    "d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
                    NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
                    ), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L)), .Names = c("Day",
                    "A1", "A2", "A3"), class = "data.frame", row.names = c(NA, -10L
                    ))
                    long_format <- melt(df)
                    colnames(long_format)[2:3] <- c("Animal","Count")





                    share|improve this answer








                    New contributor




                    seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.

















                    • How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
                      – jjulip
                      2 days ago










                    • @jjulip you can try ?melt.data.frame to get more information on the function. See e.g. under Arguments and id.vars.
                      – seth-1
                      2 days ago














                    up vote
                    2
                    down vote













                    You want to format your data frame from a wide format to a long format using the melt function of the reshape 2 package also answered here



                    library(reshape2)

                    df <- structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
                    "d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
                    NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
                    ), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L)), .Names = c("Day",
                    "A1", "A2", "A3"), class = "data.frame", row.names = c(NA, -10L
                    ))
                    long_format <- melt(df)
                    colnames(long_format)[2:3] <- c("Animal","Count")





                    share|improve this answer








                    New contributor




                    seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.

















                    • How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
                      – jjulip
                      2 days ago










                    • @jjulip you can try ?melt.data.frame to get more information on the function. See e.g. under Arguments and id.vars.
                      – seth-1
                      2 days ago












                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    You want to format your data frame from a wide format to a long format using the melt function of the reshape 2 package also answered here



                    library(reshape2)

                    df <- structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
                    "d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
                    NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
                    ), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L)), .Names = c("Day",
                    "A1", "A2", "A3"), class = "data.frame", row.names = c(NA, -10L
                    ))
                    long_format <- melt(df)
                    colnames(long_format)[2:3] <- c("Animal","Count")





                    share|improve this answer








                    New contributor




                    seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.









                    You want to format your data frame from a wide format to a long format using the melt function of the reshape 2 package also answered here



                    library(reshape2)

                    df <- structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
                    "d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
                    NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
                    ), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L)), .Names = c("Day",
                    "A1", "A2", "A3"), class = "data.frame", row.names = c(NA, -10L
                    ))
                    long_format <- melt(df)
                    colnames(long_format)[2:3] <- c("Animal","Count")






                    share|improve this answer








                    New contributor




                    seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.









                    share|improve this answer



                    share|improve this answer






                    New contributor




                    seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.









                    answered 2 days ago









                    seth-1

                    212




                    212




                    New contributor




                    seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.





                    New contributor





                    seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.






                    seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.











                    • How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
                      – jjulip
                      2 days ago










                    • @jjulip you can try ?melt.data.frame to get more information on the function. See e.g. under Arguments and id.vars.
                      – seth-1
                      2 days ago
















                    • How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
                      – jjulip
                      2 days ago










                    • @jjulip you can try ?melt.data.frame to get more information on the function. See e.g. under Arguments and id.vars.
                      – seth-1
                      2 days ago















                    How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
                    – jjulip
                    2 days ago




                    How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
                    – jjulip
                    2 days ago












                    @jjulip you can try ?melt.data.frame to get more information on the function. See e.g. under Arguments and id.vars.
                    – seth-1
                    2 days ago




                    @jjulip you can try ?melt.data.frame to get more information on the function. See e.g. under Arguments and id.vars.
                    – seth-1
                    2 days ago



                    Popular posts from this blog

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

                    How do I collapse sections of code in Visual Studio Code for Windows?