Non-structured table to narrow format table transformation?












-6















I need to make the data go from Example 1 to Example 2.



# Example 1
data.frame(Player = c("Brad Abbey", "Wins", "Losses", "Neither", "Caleb Aekins", "Wins", "Losses", "Neither"),
No = c(1, NA, NA, NA))

# Player No
# 1 Brad Abbey 1
# 2 Wins NA
# 3 Losses NA
# 4 Neither NA
# 5 Caleb Aekins 1
# 6 Wins NA
# 7 Losses NA
# 8 Neither NA


# Example 2
structure(list(Player = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("Brad Abbey", "Caleb Aekins"), class = "factor"),
Result = structure(c(3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L), class = "factor",
.Label = c("Losses",
"Neither", "Overall", "Wins"))), class = "data.frame", row.names = c(NA,
-8L))

# Player Result
# 1 Brad Abbey Overall
# 2 Brad Abbey Wins
# 3 Brad Abbey Losses
# 4 Brad Abbey Neither
# 5 Caleb Aekins Overall
# 6 Caleb Aekins Wins
# 7 Caleb Aekins Losses
# 8 Caleb Aekins Neither


How can I move 'wins', 'losses' and 'neither' to the column next to it, while copying the person's name down to the rows below it?










share|improve this question

























  • 1. Copy the Player column to a new column, Result. 2. Where Player == Result, Result = "Overall.

    – Marius
    Nov 15 '18 at 3:09






  • 1





    Please provide example data as plain text (using e.g. dput), not images, so as users can easily copy/paste it.

    – neilfws
    Nov 15 '18 at 3:12











  • Welcome to SO Adrian! Please have a read and update your question accordingly: stackoverflow.com/questions/5963269/…

    – snoram
    Nov 15 '18 at 9:28
















-6















I need to make the data go from Example 1 to Example 2.



# Example 1
data.frame(Player = c("Brad Abbey", "Wins", "Losses", "Neither", "Caleb Aekins", "Wins", "Losses", "Neither"),
No = c(1, NA, NA, NA))

# Player No
# 1 Brad Abbey 1
# 2 Wins NA
# 3 Losses NA
# 4 Neither NA
# 5 Caleb Aekins 1
# 6 Wins NA
# 7 Losses NA
# 8 Neither NA


# Example 2
structure(list(Player = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("Brad Abbey", "Caleb Aekins"), class = "factor"),
Result = structure(c(3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L), class = "factor",
.Label = c("Losses",
"Neither", "Overall", "Wins"))), class = "data.frame", row.names = c(NA,
-8L))

# Player Result
# 1 Brad Abbey Overall
# 2 Brad Abbey Wins
# 3 Brad Abbey Losses
# 4 Brad Abbey Neither
# 5 Caleb Aekins Overall
# 6 Caleb Aekins Wins
# 7 Caleb Aekins Losses
# 8 Caleb Aekins Neither


How can I move 'wins', 'losses' and 'neither' to the column next to it, while copying the person's name down to the rows below it?










share|improve this question

























  • 1. Copy the Player column to a new column, Result. 2. Where Player == Result, Result = "Overall.

    – Marius
    Nov 15 '18 at 3:09






  • 1





    Please provide example data as plain text (using e.g. dput), not images, so as users can easily copy/paste it.

    – neilfws
    Nov 15 '18 at 3:12











  • Welcome to SO Adrian! Please have a read and update your question accordingly: stackoverflow.com/questions/5963269/…

    – snoram
    Nov 15 '18 at 9:28














-6












-6








-6








I need to make the data go from Example 1 to Example 2.



# Example 1
data.frame(Player = c("Brad Abbey", "Wins", "Losses", "Neither", "Caleb Aekins", "Wins", "Losses", "Neither"),
No = c(1, NA, NA, NA))

# Player No
# 1 Brad Abbey 1
# 2 Wins NA
# 3 Losses NA
# 4 Neither NA
# 5 Caleb Aekins 1
# 6 Wins NA
# 7 Losses NA
# 8 Neither NA


# Example 2
structure(list(Player = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("Brad Abbey", "Caleb Aekins"), class = "factor"),
Result = structure(c(3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L), class = "factor",
.Label = c("Losses",
"Neither", "Overall", "Wins"))), class = "data.frame", row.names = c(NA,
-8L))

# Player Result
# 1 Brad Abbey Overall
# 2 Brad Abbey Wins
# 3 Brad Abbey Losses
# 4 Brad Abbey Neither
# 5 Caleb Aekins Overall
# 6 Caleb Aekins Wins
# 7 Caleb Aekins Losses
# 8 Caleb Aekins Neither


How can I move 'wins', 'losses' and 'neither' to the column next to it, while copying the person's name down to the rows below it?










share|improve this question
















I need to make the data go from Example 1 to Example 2.



# Example 1
data.frame(Player = c("Brad Abbey", "Wins", "Losses", "Neither", "Caleb Aekins", "Wins", "Losses", "Neither"),
No = c(1, NA, NA, NA))

# Player No
# 1 Brad Abbey 1
# 2 Wins NA
# 3 Losses NA
# 4 Neither NA
# 5 Caleb Aekins 1
# 6 Wins NA
# 7 Losses NA
# 8 Neither NA


# Example 2
structure(list(Player = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("Brad Abbey", "Caleb Aekins"), class = "factor"),
Result = structure(c(3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L), class = "factor",
.Label = c("Losses",
"Neither", "Overall", "Wins"))), class = "data.frame", row.names = c(NA,
-8L))

# Player Result
# 1 Brad Abbey Overall
# 2 Brad Abbey Wins
# 3 Brad Abbey Losses
# 4 Brad Abbey Neither
# 5 Caleb Aekins Overall
# 6 Caleb Aekins Wins
# 7 Caleb Aekins Losses
# 8 Caleb Aekins Neither


How can I move 'wins', 'losses' and 'neither' to the column next to it, while copying the person's name down to the rows below it?







r dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '18 at 9:38









Artem

1,7392727




1,7392727










asked Nov 15 '18 at 3:06









Adrian SAdrian S

1




1













  • 1. Copy the Player column to a new column, Result. 2. Where Player == Result, Result = "Overall.

    – Marius
    Nov 15 '18 at 3:09






  • 1





    Please provide example data as plain text (using e.g. dput), not images, so as users can easily copy/paste it.

    – neilfws
    Nov 15 '18 at 3:12











  • Welcome to SO Adrian! Please have a read and update your question accordingly: stackoverflow.com/questions/5963269/…

    – snoram
    Nov 15 '18 at 9:28



















  • 1. Copy the Player column to a new column, Result. 2. Where Player == Result, Result = "Overall.

    – Marius
    Nov 15 '18 at 3:09






  • 1





    Please provide example data as plain text (using e.g. dput), not images, so as users can easily copy/paste it.

    – neilfws
    Nov 15 '18 at 3:12











  • Welcome to SO Adrian! Please have a read and update your question accordingly: stackoverflow.com/questions/5963269/…

    – snoram
    Nov 15 '18 at 9:28

















1. Copy the Player column to a new column, Result. 2. Where Player == Result, Result = "Overall.

– Marius
Nov 15 '18 at 3:09





1. Copy the Player column to a new column, Result. 2. Where Player == Result, Result = "Overall.

– Marius
Nov 15 '18 at 3:09




1




1





Please provide example data as plain text (using e.g. dput), not images, so as users can easily copy/paste it.

– neilfws
Nov 15 '18 at 3:12





Please provide example data as plain text (using e.g. dput), not images, so as users can easily copy/paste it.

– neilfws
Nov 15 '18 at 3:12













Welcome to SO Adrian! Please have a read and update your question accordingly: stackoverflow.com/questions/5963269/…

– snoram
Nov 15 '18 at 9:28





Welcome to SO Adrian! Please have a read and update your question accordingly: stackoverflow.com/questions/5963269/…

– snoram
Nov 15 '18 at 9:28












1 Answer
1






active

oldest

votes


















0














You could extract players' names from the colum Players using setdiff function. Then create an output table with each player name repeated 4 times as well as cycle known Result outcomes c("Overall","Wins", "Losses", "Neither").



Please see the code below



df <- data.frame(Player = c("Brad Abbey", "Wins", "Losses", "Neither", "Caleb Aekins", "Wins", "Losses", "Neither"),
No = c(1, NA, NA, NA))

outcome <- c("Overall","Wins", "Losses", "Neither")
name <- setdiff(unique(df$Player), outcome)
res <- data.frame(Player = unlist(lapply(name, rep, 4)), Result = outcome)
res


Output:



        Player  Result
1 Brad Abbey Overall
2 Brad Abbey Wins
3 Brad Abbey Losses
4 Brad Abbey Neither
5 Caleb Aekins Overall
6 Caleb Aekins Wins
7 Caleb Aekins Losses
8 Caleb Aekins Neither





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311837%2fnon-structured-table-to-narrow-format-table-transformation%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You could extract players' names from the colum Players using setdiff function. Then create an output table with each player name repeated 4 times as well as cycle known Result outcomes c("Overall","Wins", "Losses", "Neither").



    Please see the code below



    df <- data.frame(Player = c("Brad Abbey", "Wins", "Losses", "Neither", "Caleb Aekins", "Wins", "Losses", "Neither"),
    No = c(1, NA, NA, NA))

    outcome <- c("Overall","Wins", "Losses", "Neither")
    name <- setdiff(unique(df$Player), outcome)
    res <- data.frame(Player = unlist(lapply(name, rep, 4)), Result = outcome)
    res


    Output:



            Player  Result
    1 Brad Abbey Overall
    2 Brad Abbey Wins
    3 Brad Abbey Losses
    4 Brad Abbey Neither
    5 Caleb Aekins Overall
    6 Caleb Aekins Wins
    7 Caleb Aekins Losses
    8 Caleb Aekins Neither





    share|improve this answer






























      0














      You could extract players' names from the colum Players using setdiff function. Then create an output table with each player name repeated 4 times as well as cycle known Result outcomes c("Overall","Wins", "Losses", "Neither").



      Please see the code below



      df <- data.frame(Player = c("Brad Abbey", "Wins", "Losses", "Neither", "Caleb Aekins", "Wins", "Losses", "Neither"),
      No = c(1, NA, NA, NA))

      outcome <- c("Overall","Wins", "Losses", "Neither")
      name <- setdiff(unique(df$Player), outcome)
      res <- data.frame(Player = unlist(lapply(name, rep, 4)), Result = outcome)
      res


      Output:



              Player  Result
      1 Brad Abbey Overall
      2 Brad Abbey Wins
      3 Brad Abbey Losses
      4 Brad Abbey Neither
      5 Caleb Aekins Overall
      6 Caleb Aekins Wins
      7 Caleb Aekins Losses
      8 Caleb Aekins Neither





      share|improve this answer




























        0












        0








        0







        You could extract players' names from the colum Players using setdiff function. Then create an output table with each player name repeated 4 times as well as cycle known Result outcomes c("Overall","Wins", "Losses", "Neither").



        Please see the code below



        df <- data.frame(Player = c("Brad Abbey", "Wins", "Losses", "Neither", "Caleb Aekins", "Wins", "Losses", "Neither"),
        No = c(1, NA, NA, NA))

        outcome <- c("Overall","Wins", "Losses", "Neither")
        name <- setdiff(unique(df$Player), outcome)
        res <- data.frame(Player = unlist(lapply(name, rep, 4)), Result = outcome)
        res


        Output:



                Player  Result
        1 Brad Abbey Overall
        2 Brad Abbey Wins
        3 Brad Abbey Losses
        4 Brad Abbey Neither
        5 Caleb Aekins Overall
        6 Caleb Aekins Wins
        7 Caleb Aekins Losses
        8 Caleb Aekins Neither





        share|improve this answer















        You could extract players' names from the colum Players using setdiff function. Then create an output table with each player name repeated 4 times as well as cycle known Result outcomes c("Overall","Wins", "Losses", "Neither").



        Please see the code below



        df <- data.frame(Player = c("Brad Abbey", "Wins", "Losses", "Neither", "Caleb Aekins", "Wins", "Losses", "Neither"),
        No = c(1, NA, NA, NA))

        outcome <- c("Overall","Wins", "Losses", "Neither")
        name <- setdiff(unique(df$Player), outcome)
        res <- data.frame(Player = unlist(lapply(name, rep, 4)), Result = outcome)
        res


        Output:



                Player  Result
        1 Brad Abbey Overall
        2 Brad Abbey Wins
        3 Brad Abbey Losses
        4 Brad Abbey Neither
        5 Caleb Aekins Overall
        6 Caleb Aekins Wins
        7 Caleb Aekins Losses
        8 Caleb Aekins Neither






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 19 '18 at 6:44

























        answered Dec 19 '18 at 6:22









        ArtemArtem

        1,7392727




        1,7392727






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311837%2fnon-structured-table-to-narrow-format-table-transformation%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini