Is there an R function for counting same value on a row?












0















I'm looking for a function which could gives me the number of times the same string is present on a line by returning this number in a new column with this character string as name. Let's take an example:



 df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656')
)


So, on the first line we observe twice `BF50343 and I'm looking to build new columns in order to get:



 df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656'),
ABF4576 = c(1,1,0),
AG4633 = c(0,1,0),
AWW07954 = c(0,0,1),
ABF5403 = c(1,0,0),
A64ED56 = c(0,0,1),
BF50343 = c(2,0,0),
BGF4761 = c(0,1,0),
B76WW56 = c(0,0,1),
BDZ4641 = c(0,1,0),
B917656 = c(0,0,1)
)


If you have any idea to develop, thanks for your time










share|improve this question

























  • I can't try rowSums(df == "BF50343") because there is a lot of different values.

    – Alex Germain
    Jan 2 at 10:11











  • Should I try with str_count()?

    – Alex Germain
    Jan 3 at 10:24
















0















I'm looking for a function which could gives me the number of times the same string is present on a line by returning this number in a new column with this character string as name. Let's take an example:



 df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656')
)


So, on the first line we observe twice `BF50343 and I'm looking to build new columns in order to get:



 df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656'),
ABF4576 = c(1,1,0),
AG4633 = c(0,1,0),
AWW07954 = c(0,0,1),
ABF5403 = c(1,0,0),
A64ED56 = c(0,0,1),
BF50343 = c(2,0,0),
BGF4761 = c(0,1,0),
B76WW56 = c(0,0,1),
BDZ4641 = c(0,1,0),
B917656 = c(0,0,1)
)


If you have any idea to develop, thanks for your time










share|improve this question

























  • I can't try rowSums(df == "BF50343") because there is a lot of different values.

    – Alex Germain
    Jan 2 at 10:11











  • Should I try with str_count()?

    – Alex Germain
    Jan 3 at 10:24














0












0








0


1






I'm looking for a function which could gives me the number of times the same string is present on a line by returning this number in a new column with this character string as name. Let's take an example:



 df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656')
)


So, on the first line we observe twice `BF50343 and I'm looking to build new columns in order to get:



 df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656'),
ABF4576 = c(1,1,0),
AG4633 = c(0,1,0),
AWW07954 = c(0,0,1),
ABF5403 = c(1,0,0),
A64ED56 = c(0,0,1),
BF50343 = c(2,0,0),
BGF4761 = c(0,1,0),
B76WW56 = c(0,0,1),
BDZ4641 = c(0,1,0),
B917656 = c(0,0,1)
)


If you have any idea to develop, thanks for your time










share|improve this question
















I'm looking for a function which could gives me the number of times the same string is present on a line by returning this number in a new column with this character string as name. Let's take an example:



 df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656')
)


So, on the first line we observe twice `BF50343 and I'm looking to build new columns in order to get:



 df <- data.frame(
Year = rnorm(3),
hour = rnorm(3),
LOT = rnorm(3),
S123_AA = c('ABF4576','AG4633','AWW07954'),
S135_AA = c('ABF5403','ABF4576','A64ED56'),
S1763_BB = c('BF50343','BGF4761','B76WW56'),
S173_BB = c('BF50343','BDZ4641','B917656'),
ABF4576 = c(1,1,0),
AG4633 = c(0,1,0),
AWW07954 = c(0,0,1),
ABF5403 = c(1,0,0),
A64ED56 = c(0,0,1),
BF50343 = c(2,0,0),
BGF4761 = c(0,1,0),
B76WW56 = c(0,0,1),
BDZ4641 = c(0,1,0),
B917656 = c(0,0,1)
)


If you have any idea to develop, thanks for your time







r dataframe count row






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 18:22









markus

14.7k11336




14.7k11336










asked Jan 2 at 9:46









Alex GermainAlex Germain

778




778













  • I can't try rowSums(df == "BF50343") because there is a lot of different values.

    – Alex Germain
    Jan 2 at 10:11











  • Should I try with str_count()?

    – Alex Germain
    Jan 3 at 10:24



















  • I can't try rowSums(df == "BF50343") because there is a lot of different values.

    – Alex Germain
    Jan 2 at 10:11











  • Should I try with str_count()?

    – Alex Germain
    Jan 3 at 10:24

















I can't try rowSums(df == "BF50343") because there is a lot of different values.

– Alex Germain
Jan 2 at 10:11





I can't try rowSums(df == "BF50343") because there is a lot of different values.

– Alex Germain
Jan 2 at 10:11













Should I try with str_count()?

– Alex Germain
Jan 3 at 10:24





Should I try with str_count()?

– Alex Germain
Jan 3 at 10:24












1 Answer
1






active

oldest

votes


















1














You can use lapply to loop over the unique values of your character variables:



cols <- !(colnames(df) %in% c("Year", "hour", "LOT")) ## variables of interest
vals <- as.character(unique(unlist(df[cols]))) ## unique values
res <- do.call("cbind", lapply(vals, function(x) rowSums(df[cols] == x)))
colnames(res) <- vals
df <- cbind(df, res)





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%2f54004149%2fis-there-an-r-function-for-counting-same-value-on-a-row%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









    1














    You can use lapply to loop over the unique values of your character variables:



    cols <- !(colnames(df) %in% c("Year", "hour", "LOT")) ## variables of interest
    vals <- as.character(unique(unlist(df[cols]))) ## unique values
    res <- do.call("cbind", lapply(vals, function(x) rowSums(df[cols] == x)))
    colnames(res) <- vals
    df <- cbind(df, res)





    share|improve this answer




























      1














      You can use lapply to loop over the unique values of your character variables:



      cols <- !(colnames(df) %in% c("Year", "hour", "LOT")) ## variables of interest
      vals <- as.character(unique(unlist(df[cols]))) ## unique values
      res <- do.call("cbind", lapply(vals, function(x) rowSums(df[cols] == x)))
      colnames(res) <- vals
      df <- cbind(df, res)





      share|improve this answer


























        1












        1








        1







        You can use lapply to loop over the unique values of your character variables:



        cols <- !(colnames(df) %in% c("Year", "hour", "LOT")) ## variables of interest
        vals <- as.character(unique(unlist(df[cols]))) ## unique values
        res <- do.call("cbind", lapply(vals, function(x) rowSums(df[cols] == x)))
        colnames(res) <- vals
        df <- cbind(df, res)





        share|improve this answer













        You can use lapply to loop over the unique values of your character variables:



        cols <- !(colnames(df) %in% c("Year", "hour", "LOT")) ## variables of interest
        vals <- as.character(unique(unlist(df[cols]))) ## unique values
        res <- do.call("cbind", lapply(vals, function(x) rowSums(df[cols] == x)))
        colnames(res) <- vals
        df <- cbind(df, res)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 9 at 15:24









        Balint T.Balint T.

        513




        513
































            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%2f54004149%2fis-there-an-r-function-for-counting-same-value-on-a-row%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







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith