How to make this code more efficient in R?












0















I know this is a stupid question, but I'm kinda frustrated with my code because it takes so much time. Jere is one part of my code.
basically I have a matrix called "distance"...



   a  b  c  
1 2 5 7
2 6 8 4
3 9 2 3


and then lets say I have a column in a data frame, contains of {a,b,c}



c1  c2  c3
c ... ...
a
a just another column
b
c ... ...


so I want to do a match, I wanna make another matrix with ncol=nrow(distance), and nrow=nrow(c1). where replace the factor value with their distance value. Here's an example of the first column of matrix that I'm going to make



a will replaced by 2 
b will replaced by 5
c will replaced by 7


and for the second column, i will take row number 2 from distance matrix, and so on... so the result will be like this



m1  m2  m3
7 4 3
2 6 9
2 6 9
5 8 2
7 4 3


That is just an easy example, and I'm running this code, but when it deals with large iterations, it's kinda stressful for me.



for(l in 1:ncol(d.cat)){
get.unique = sort(unique(d.cat[, l]))
for(j in 1:nrow(d.cat)){
value = as.character(d.cat[j, l])
index = which(get.unique == value)
d2[j,l] = (d[[l]][i, index])
}
}


d.cat is categorical data. And d[[...]] is the list of matrix distance for every column in d.cat.










share|improve this question

























  • I removed the [rcpp] tag as this has nothing to do with Rcpp.

    – Dirk Eddelbuettel
    Mar 21 '18 at 22:28
















0















I know this is a stupid question, but I'm kinda frustrated with my code because it takes so much time. Jere is one part of my code.
basically I have a matrix called "distance"...



   a  b  c  
1 2 5 7
2 6 8 4
3 9 2 3


and then lets say I have a column in a data frame, contains of {a,b,c}



c1  c2  c3
c ... ...
a
a just another column
b
c ... ...


so I want to do a match, I wanna make another matrix with ncol=nrow(distance), and nrow=nrow(c1). where replace the factor value with their distance value. Here's an example of the first column of matrix that I'm going to make



a will replaced by 2 
b will replaced by 5
c will replaced by 7


and for the second column, i will take row number 2 from distance matrix, and so on... so the result will be like this



m1  m2  m3
7 4 3
2 6 9
2 6 9
5 8 2
7 4 3


That is just an easy example, and I'm running this code, but when it deals with large iterations, it's kinda stressful for me.



for(l in 1:ncol(d.cat)){
get.unique = sort(unique(d.cat[, l]))
for(j in 1:nrow(d.cat)){
value = as.character(d.cat[j, l])
index = which(get.unique == value)
d2[j,l] = (d[[l]][i, index])
}
}


d.cat is categorical data. And d[[...]] is the list of matrix distance for every column in d.cat.










share|improve this question

























  • I removed the [rcpp] tag as this has nothing to do with Rcpp.

    – Dirk Eddelbuettel
    Mar 21 '18 at 22:28














0












0








0


1






I know this is a stupid question, but I'm kinda frustrated with my code because it takes so much time. Jere is one part of my code.
basically I have a matrix called "distance"...



   a  b  c  
1 2 5 7
2 6 8 4
3 9 2 3


and then lets say I have a column in a data frame, contains of {a,b,c}



c1  c2  c3
c ... ...
a
a just another column
b
c ... ...


so I want to do a match, I wanna make another matrix with ncol=nrow(distance), and nrow=nrow(c1). where replace the factor value with their distance value. Here's an example of the first column of matrix that I'm going to make



a will replaced by 2 
b will replaced by 5
c will replaced by 7


and for the second column, i will take row number 2 from distance matrix, and so on... so the result will be like this



m1  m2  m3
7 4 3
2 6 9
2 6 9
5 8 2
7 4 3


That is just an easy example, and I'm running this code, but when it deals with large iterations, it's kinda stressful for me.



for(l in 1:ncol(d.cat)){
get.unique = sort(unique(d.cat[, l]))
for(j in 1:nrow(d.cat)){
value = as.character(d.cat[j, l])
index = which(get.unique == value)
d2[j,l] = (d[[l]][i, index])
}
}


d.cat is categorical data. And d[[...]] is the list of matrix distance for every column in d.cat.










share|improve this question
















I know this is a stupid question, but I'm kinda frustrated with my code because it takes so much time. Jere is one part of my code.
basically I have a matrix called "distance"...



   a  b  c  
1 2 5 7
2 6 8 4
3 9 2 3


and then lets say I have a column in a data frame, contains of {a,b,c}



c1  c2  c3
c ... ...
a
a just another column
b
c ... ...


so I want to do a match, I wanna make another matrix with ncol=nrow(distance), and nrow=nrow(c1). where replace the factor value with their distance value. Here's an example of the first column of matrix that I'm going to make



a will replaced by 2 
b will replaced by 5
c will replaced by 7


and for the second column, i will take row number 2 from distance matrix, and so on... so the result will be like this



m1  m2  m3
7 4 3
2 6 9
2 6 9
5 8 2
7 4 3


That is just an easy example, and I'm running this code, but when it deals with large iterations, it's kinda stressful for me.



for(l in 1:ncol(d.cat)){
get.unique = sort(unique(d.cat[, l]))
for(j in 1:nrow(d.cat)){
value = as.character(d.cat[j, l])
index = which(get.unique == value)
d2[j,l] = (d[[l]][i, index])
}
}


d.cat is categorical data. And d[[...]] is the list of matrix distance for every column in d.cat.







r performance dataframe matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 6:53









Marcus Campbell

2,08331228




2,08331228










asked Mar 21 '18 at 18:12









Jack shephardJack shephard

256




256













  • I removed the [rcpp] tag as this has nothing to do with Rcpp.

    – Dirk Eddelbuettel
    Mar 21 '18 at 22:28



















  • I removed the [rcpp] tag as this has nothing to do with Rcpp.

    – Dirk Eddelbuettel
    Mar 21 '18 at 22:28

















I removed the [rcpp] tag as this has nothing to do with Rcpp.

– Dirk Eddelbuettel
Mar 21 '18 at 22:28





I removed the [rcpp] tag as this has nothing to do with Rcpp.

– Dirk Eddelbuettel
Mar 21 '18 at 22:28












4 Answers
4






active

oldest

votes


















1














Here's some data



set.seed(123)
d = matrix(1:9, 3, dimnames=list(NULL, letters[1:3]))
df = data.frame(c1 = sample(letters[1:3], 10, TRUE), stringsAsFactors=FALSE)


and a solution



t(d[, match(df$c1, colnames(d))])


For example



> d
a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> df$c1
[1] "a" "c" "b" "c" "c" "a" "b" "c" "b" "b"
> t(d[,match(df$c1, colnames(d))])
[,1] [,2] [,3]
a 1 2 3
c 7 8 9
b 4 5 6
c 7 8 9
c 7 8 9
a 1 2 3
b 4 5 6
c 7 8 9
b 4 5 6
b 4 5 6





share|improve this answer
























  • i like this way, thank u so much for your help

    – Jack shephard
    Mar 22 '18 at 6:18











  • with your way make my code running in 1.7 minutes before, and now just 9 seconds... thank u so much

    – Jack shephard
    Mar 22 '18 at 7:32



















2














Try to store the indices and do the updating in one go. Lets say your distance matrix is dmat and data frame is df and you want to create a matrix named newmat



a.ind = which(df$c1=="a")
b.ind = which(df$c1=="b")
c.ind = which(df$c1=="c")
newmat = matrix(0,nrow=length(df$c1),ncol=3)
newmat[a.ind,] = dmat[,1]
newmat[b.ind,] = dmat[,2]
newmat[c.ind,] = dmat[,3]





share|improve this answer
























  • thank u so much for this help

    – Jack shephard
    Mar 22 '18 at 6:18



















1














Your data



mat <- matrix(c(2,6,9,5,8,2,7,4,3), nrow=3)
rownames(mat) <- 1:3
colnames(mat) <- letters[1:3]

library(dplyr)
set.seed(1)
df <- as.data.frame(matrix(sample(letters[1:3], 12, replace=TRUE), nrow=4)) %>%
setNames(paste0("c", 1:3))

# c1 c2 c3
# 1 a a b
# 2 b c a
# 3 b c a
# 4 c b a


Using purrr::map2_df, iterate through columns of df and columns of tmat



library(purrr)
tmat <- t(mat)
map2_df(df, seq_len(ncol(tmat)), ~tmat[,.y][.x])

# # A tibble: 4 x 3
# c1 c2 c3
# <dbl> <dbl> <dbl>
# 1 2. 6. 2.
# 2 5. 4. 9.
# 3 5. 4. 9.
# 4 7. 8. 9.





share|improve this answer































    1














    Here is my attempt using the tidyverse :



    library(tidyverse)

    # Lets create some example
    distance <- data_frame(a = sample(1:10, 1000, T), b = sample(1:10, 1000, T), c = sample(1:10, 1000, T))
    c1 <- data_frame(c1 = sample(letters[1:3], 1000, T), c2 = sample(letters[1:3], 1000, T))

    # First rearrange a little bit your data to make it more tidy
    distance2 <- distance %>%
    mutate(i = seq_len(n())) %>%
    gather(col, value, -i)
    c2 <- c1 %>%
    mutate(i = seq_len(n()) %>%
    gather(col, value, -i)

    # Now just join the data and spread it again
    c12 %>%
    left_join(distance2, by = c("i", "value" = "col")) %>%
    select(i, col, value.y) %>%
    spread(col, value.y)





    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%2f49413782%2fhow-to-make-this-code-more-efficient-in-r%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Here's some data



      set.seed(123)
      d = matrix(1:9, 3, dimnames=list(NULL, letters[1:3]))
      df = data.frame(c1 = sample(letters[1:3], 10, TRUE), stringsAsFactors=FALSE)


      and a solution



      t(d[, match(df$c1, colnames(d))])


      For example



      > d
      a b c
      [1,] 1 4 7
      [2,] 2 5 8
      [3,] 3 6 9
      > df$c1
      [1] "a" "c" "b" "c" "c" "a" "b" "c" "b" "b"
      > t(d[,match(df$c1, colnames(d))])
      [,1] [,2] [,3]
      a 1 2 3
      c 7 8 9
      b 4 5 6
      c 7 8 9
      c 7 8 9
      a 1 2 3
      b 4 5 6
      c 7 8 9
      b 4 5 6
      b 4 5 6





      share|improve this answer
























      • i like this way, thank u so much for your help

        – Jack shephard
        Mar 22 '18 at 6:18











      • with your way make my code running in 1.7 minutes before, and now just 9 seconds... thank u so much

        – Jack shephard
        Mar 22 '18 at 7:32
















      1














      Here's some data



      set.seed(123)
      d = matrix(1:9, 3, dimnames=list(NULL, letters[1:3]))
      df = data.frame(c1 = sample(letters[1:3], 10, TRUE), stringsAsFactors=FALSE)


      and a solution



      t(d[, match(df$c1, colnames(d))])


      For example



      > d
      a b c
      [1,] 1 4 7
      [2,] 2 5 8
      [3,] 3 6 9
      > df$c1
      [1] "a" "c" "b" "c" "c" "a" "b" "c" "b" "b"
      > t(d[,match(df$c1, colnames(d))])
      [,1] [,2] [,3]
      a 1 2 3
      c 7 8 9
      b 4 5 6
      c 7 8 9
      c 7 8 9
      a 1 2 3
      b 4 5 6
      c 7 8 9
      b 4 5 6
      b 4 5 6





      share|improve this answer
























      • i like this way, thank u so much for your help

        – Jack shephard
        Mar 22 '18 at 6:18











      • with your way make my code running in 1.7 minutes before, and now just 9 seconds... thank u so much

        – Jack shephard
        Mar 22 '18 at 7:32














      1












      1








      1







      Here's some data



      set.seed(123)
      d = matrix(1:9, 3, dimnames=list(NULL, letters[1:3]))
      df = data.frame(c1 = sample(letters[1:3], 10, TRUE), stringsAsFactors=FALSE)


      and a solution



      t(d[, match(df$c1, colnames(d))])


      For example



      > d
      a b c
      [1,] 1 4 7
      [2,] 2 5 8
      [3,] 3 6 9
      > df$c1
      [1] "a" "c" "b" "c" "c" "a" "b" "c" "b" "b"
      > t(d[,match(df$c1, colnames(d))])
      [,1] [,2] [,3]
      a 1 2 3
      c 7 8 9
      b 4 5 6
      c 7 8 9
      c 7 8 9
      a 1 2 3
      b 4 5 6
      c 7 8 9
      b 4 5 6
      b 4 5 6





      share|improve this answer













      Here's some data



      set.seed(123)
      d = matrix(1:9, 3, dimnames=list(NULL, letters[1:3]))
      df = data.frame(c1 = sample(letters[1:3], 10, TRUE), stringsAsFactors=FALSE)


      and a solution



      t(d[, match(df$c1, colnames(d))])


      For example



      > d
      a b c
      [1,] 1 4 7
      [2,] 2 5 8
      [3,] 3 6 9
      > df$c1
      [1] "a" "c" "b" "c" "c" "a" "b" "c" "b" "b"
      > t(d[,match(df$c1, colnames(d))])
      [,1] [,2] [,3]
      a 1 2 3
      c 7 8 9
      b 4 5 6
      c 7 8 9
      c 7 8 9
      a 1 2 3
      b 4 5 6
      c 7 8 9
      b 4 5 6
      b 4 5 6






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 21 '18 at 20:40









      Martin MorganMartin Morgan

      39.1k36092




      39.1k36092













      • i like this way, thank u so much for your help

        – Jack shephard
        Mar 22 '18 at 6:18











      • with your way make my code running in 1.7 minutes before, and now just 9 seconds... thank u so much

        – Jack shephard
        Mar 22 '18 at 7:32



















      • i like this way, thank u so much for your help

        – Jack shephard
        Mar 22 '18 at 6:18











      • with your way make my code running in 1.7 minutes before, and now just 9 seconds... thank u so much

        – Jack shephard
        Mar 22 '18 at 7:32

















      i like this way, thank u so much for your help

      – Jack shephard
      Mar 22 '18 at 6:18





      i like this way, thank u so much for your help

      – Jack shephard
      Mar 22 '18 at 6:18













      with your way make my code running in 1.7 minutes before, and now just 9 seconds... thank u so much

      – Jack shephard
      Mar 22 '18 at 7:32





      with your way make my code running in 1.7 minutes before, and now just 9 seconds... thank u so much

      – Jack shephard
      Mar 22 '18 at 7:32













      2














      Try to store the indices and do the updating in one go. Lets say your distance matrix is dmat and data frame is df and you want to create a matrix named newmat



      a.ind = which(df$c1=="a")
      b.ind = which(df$c1=="b")
      c.ind = which(df$c1=="c")
      newmat = matrix(0,nrow=length(df$c1),ncol=3)
      newmat[a.ind,] = dmat[,1]
      newmat[b.ind,] = dmat[,2]
      newmat[c.ind,] = dmat[,3]





      share|improve this answer
























      • thank u so much for this help

        – Jack shephard
        Mar 22 '18 at 6:18
















      2














      Try to store the indices and do the updating in one go. Lets say your distance matrix is dmat and data frame is df and you want to create a matrix named newmat



      a.ind = which(df$c1=="a")
      b.ind = which(df$c1=="b")
      c.ind = which(df$c1=="c")
      newmat = matrix(0,nrow=length(df$c1),ncol=3)
      newmat[a.ind,] = dmat[,1]
      newmat[b.ind,] = dmat[,2]
      newmat[c.ind,] = dmat[,3]





      share|improve this answer
























      • thank u so much for this help

        – Jack shephard
        Mar 22 '18 at 6:18














      2












      2








      2







      Try to store the indices and do the updating in one go. Lets say your distance matrix is dmat and data frame is df and you want to create a matrix named newmat



      a.ind = which(df$c1=="a")
      b.ind = which(df$c1=="b")
      c.ind = which(df$c1=="c")
      newmat = matrix(0,nrow=length(df$c1),ncol=3)
      newmat[a.ind,] = dmat[,1]
      newmat[b.ind,] = dmat[,2]
      newmat[c.ind,] = dmat[,3]





      share|improve this answer













      Try to store the indices and do the updating in one go. Lets say your distance matrix is dmat and data frame is df and you want to create a matrix named newmat



      a.ind = which(df$c1=="a")
      b.ind = which(df$c1=="b")
      c.ind = which(df$c1=="c")
      newmat = matrix(0,nrow=length(df$c1),ncol=3)
      newmat[a.ind,] = dmat[,1]
      newmat[b.ind,] = dmat[,2]
      newmat[c.ind,] = dmat[,3]






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 21 '18 at 18:32









      bala83bala83

      39537




      39537













      • thank u so much for this help

        – Jack shephard
        Mar 22 '18 at 6:18



















      • thank u so much for this help

        – Jack shephard
        Mar 22 '18 at 6:18

















      thank u so much for this help

      – Jack shephard
      Mar 22 '18 at 6:18





      thank u so much for this help

      – Jack shephard
      Mar 22 '18 at 6:18











      1














      Your data



      mat <- matrix(c(2,6,9,5,8,2,7,4,3), nrow=3)
      rownames(mat) <- 1:3
      colnames(mat) <- letters[1:3]

      library(dplyr)
      set.seed(1)
      df <- as.data.frame(matrix(sample(letters[1:3], 12, replace=TRUE), nrow=4)) %>%
      setNames(paste0("c", 1:3))

      # c1 c2 c3
      # 1 a a b
      # 2 b c a
      # 3 b c a
      # 4 c b a


      Using purrr::map2_df, iterate through columns of df and columns of tmat



      library(purrr)
      tmat <- t(mat)
      map2_df(df, seq_len(ncol(tmat)), ~tmat[,.y][.x])

      # # A tibble: 4 x 3
      # c1 c2 c3
      # <dbl> <dbl> <dbl>
      # 1 2. 6. 2.
      # 2 5. 4. 9.
      # 3 5. 4. 9.
      # 4 7. 8. 9.





      share|improve this answer




























        1














        Your data



        mat <- matrix(c(2,6,9,5,8,2,7,4,3), nrow=3)
        rownames(mat) <- 1:3
        colnames(mat) <- letters[1:3]

        library(dplyr)
        set.seed(1)
        df <- as.data.frame(matrix(sample(letters[1:3], 12, replace=TRUE), nrow=4)) %>%
        setNames(paste0("c", 1:3))

        # c1 c2 c3
        # 1 a a b
        # 2 b c a
        # 3 b c a
        # 4 c b a


        Using purrr::map2_df, iterate through columns of df and columns of tmat



        library(purrr)
        tmat <- t(mat)
        map2_df(df, seq_len(ncol(tmat)), ~tmat[,.y][.x])

        # # A tibble: 4 x 3
        # c1 c2 c3
        # <dbl> <dbl> <dbl>
        # 1 2. 6. 2.
        # 2 5. 4. 9.
        # 3 5. 4. 9.
        # 4 7. 8. 9.





        share|improve this answer


























          1












          1








          1







          Your data



          mat <- matrix(c(2,6,9,5,8,2,7,4,3), nrow=3)
          rownames(mat) <- 1:3
          colnames(mat) <- letters[1:3]

          library(dplyr)
          set.seed(1)
          df <- as.data.frame(matrix(sample(letters[1:3], 12, replace=TRUE), nrow=4)) %>%
          setNames(paste0("c", 1:3))

          # c1 c2 c3
          # 1 a a b
          # 2 b c a
          # 3 b c a
          # 4 c b a


          Using purrr::map2_df, iterate through columns of df and columns of tmat



          library(purrr)
          tmat <- t(mat)
          map2_df(df, seq_len(ncol(tmat)), ~tmat[,.y][.x])

          # # A tibble: 4 x 3
          # c1 c2 c3
          # <dbl> <dbl> <dbl>
          # 1 2. 6. 2.
          # 2 5. 4. 9.
          # 3 5. 4. 9.
          # 4 7. 8. 9.





          share|improve this answer













          Your data



          mat <- matrix(c(2,6,9,5,8,2,7,4,3), nrow=3)
          rownames(mat) <- 1:3
          colnames(mat) <- letters[1:3]

          library(dplyr)
          set.seed(1)
          df <- as.data.frame(matrix(sample(letters[1:3], 12, replace=TRUE), nrow=4)) %>%
          setNames(paste0("c", 1:3))

          # c1 c2 c3
          # 1 a a b
          # 2 b c a
          # 3 b c a
          # 4 c b a


          Using purrr::map2_df, iterate through columns of df and columns of tmat



          library(purrr)
          tmat <- t(mat)
          map2_df(df, seq_len(ncol(tmat)), ~tmat[,.y][.x])

          # # A tibble: 4 x 3
          # c1 c2 c3
          # <dbl> <dbl> <dbl>
          # 1 2. 6. 2.
          # 2 5. 4. 9.
          # 3 5. 4. 9.
          # 4 7. 8. 9.






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 21 '18 at 18:39









          CPakCPak

          9,5841727




          9,5841727























              1














              Here is my attempt using the tidyverse :



              library(tidyverse)

              # Lets create some example
              distance <- data_frame(a = sample(1:10, 1000, T), b = sample(1:10, 1000, T), c = sample(1:10, 1000, T))
              c1 <- data_frame(c1 = sample(letters[1:3], 1000, T), c2 = sample(letters[1:3], 1000, T))

              # First rearrange a little bit your data to make it more tidy
              distance2 <- distance %>%
              mutate(i = seq_len(n())) %>%
              gather(col, value, -i)
              c2 <- c1 %>%
              mutate(i = seq_len(n()) %>%
              gather(col, value, -i)

              # Now just join the data and spread it again
              c12 %>%
              left_join(distance2, by = c("i", "value" = "col")) %>%
              select(i, col, value.y) %>%
              spread(col, value.y)





              share|improve this answer




























                1














                Here is my attempt using the tidyverse :



                library(tidyverse)

                # Lets create some example
                distance <- data_frame(a = sample(1:10, 1000, T), b = sample(1:10, 1000, T), c = sample(1:10, 1000, T))
                c1 <- data_frame(c1 = sample(letters[1:3], 1000, T), c2 = sample(letters[1:3], 1000, T))

                # First rearrange a little bit your data to make it more tidy
                distance2 <- distance %>%
                mutate(i = seq_len(n())) %>%
                gather(col, value, -i)
                c2 <- c1 %>%
                mutate(i = seq_len(n()) %>%
                gather(col, value, -i)

                # Now just join the data and spread it again
                c12 %>%
                left_join(distance2, by = c("i", "value" = "col")) %>%
                select(i, col, value.y) %>%
                spread(col, value.y)





                share|improve this answer


























                  1












                  1








                  1







                  Here is my attempt using the tidyverse :



                  library(tidyverse)

                  # Lets create some example
                  distance <- data_frame(a = sample(1:10, 1000, T), b = sample(1:10, 1000, T), c = sample(1:10, 1000, T))
                  c1 <- data_frame(c1 = sample(letters[1:3], 1000, T), c2 = sample(letters[1:3], 1000, T))

                  # First rearrange a little bit your data to make it more tidy
                  distance2 <- distance %>%
                  mutate(i = seq_len(n())) %>%
                  gather(col, value, -i)
                  c2 <- c1 %>%
                  mutate(i = seq_len(n()) %>%
                  gather(col, value, -i)

                  # Now just join the data and spread it again
                  c12 %>%
                  left_join(distance2, by = c("i", "value" = "col")) %>%
                  select(i, col, value.y) %>%
                  spread(col, value.y)





                  share|improve this answer













                  Here is my attempt using the tidyverse :



                  library(tidyverse)

                  # Lets create some example
                  distance <- data_frame(a = sample(1:10, 1000, T), b = sample(1:10, 1000, T), c = sample(1:10, 1000, T))
                  c1 <- data_frame(c1 = sample(letters[1:3], 1000, T), c2 = sample(letters[1:3], 1000, T))

                  # First rearrange a little bit your data to make it more tidy
                  distance2 <- distance %>%
                  mutate(i = seq_len(n())) %>%
                  gather(col, value, -i)
                  c2 <- c1 %>%
                  mutate(i = seq_len(n()) %>%
                  gather(col, value, -i)

                  # Now just join the data and spread it again
                  c12 %>%
                  left_join(distance2, by = c("i", "value" = "col")) %>%
                  select(i, col, value.y) %>%
                  spread(col, value.y)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 21 '18 at 18:54









                  denroudenrou

                  532211




                  532211






























                      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%2f49413782%2fhow-to-make-this-code-more-efficient-in-r%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

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      Origin of the phrase “under your belt”?