Extracting and using information from other rows in a dataframe?












0















So I have a data frame like the following:



id age  friend1 friend2
01 15 02 05
02 23 01 05
03 51 04
04 41 03
05 33 01 02


How can I calculate the average age of the friends and create a new column that stores this information? So ideally it looks something like this:



id age  friend1 friend2 AvgAgeF
01 15 02 05 28
02 23 01 05 24
03 51 04 41
04 41 03 51
05 33 01 02 19


Currently I can run the following code to do this:



inx <- grep("friend", names(dat))
tmp <- sapply(inx, function(i) dat$age[dat[[i]]])
dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)


However, when the "id" for the people aren't in order or aren't numbers at all such as in the following example:



dat <- read.table(text = "
id age friend1 friend2
Bob 15 Jack Sam
Jack 23 Sam Bob
Sam 51 Bob Jack
Sara 41 Henry NA
Henry 33 Sara NA
", header = TRUE)


The code appears to process incorrectly resulting in the following table (All but Sam's AvgAgeF is incorrect in this case):



     id age friend1 friend2 AvgAgeF
1 Bob 15 Jack Sam 51
2 Jack 23 Sam Bob 28
3 Sam 51 Bob Jack 19
4 Sara 41 Henry <NA> 23
5 Henry 33 Sara <NA> 33


How can I solve for this without changing the Names into numerical ids.










share|improve this question





























    0















    So I have a data frame like the following:



    id age  friend1 friend2
    01 15 02 05
    02 23 01 05
    03 51 04
    04 41 03
    05 33 01 02


    How can I calculate the average age of the friends and create a new column that stores this information? So ideally it looks something like this:



    id age  friend1 friend2 AvgAgeF
    01 15 02 05 28
    02 23 01 05 24
    03 51 04 41
    04 41 03 51
    05 33 01 02 19


    Currently I can run the following code to do this:



    inx <- grep("friend", names(dat))
    tmp <- sapply(inx, function(i) dat$age[dat[[i]]])
    dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)


    However, when the "id" for the people aren't in order or aren't numbers at all such as in the following example:



    dat <- read.table(text = "
    id age friend1 friend2
    Bob 15 Jack Sam
    Jack 23 Sam Bob
    Sam 51 Bob Jack
    Sara 41 Henry NA
    Henry 33 Sara NA
    ", header = TRUE)


    The code appears to process incorrectly resulting in the following table (All but Sam's AvgAgeF is incorrect in this case):



         id age friend1 friend2 AvgAgeF
    1 Bob 15 Jack Sam 51
    2 Jack 23 Sam Bob 28
    3 Sam 51 Bob Jack 19
    4 Sara 41 Henry <NA> 23
    5 Henry 33 Sara <NA> 33


    How can I solve for this without changing the Names into numerical ids.










    share|improve this question



























      0












      0








      0


      0






      So I have a data frame like the following:



      id age  friend1 friend2
      01 15 02 05
      02 23 01 05
      03 51 04
      04 41 03
      05 33 01 02


      How can I calculate the average age of the friends and create a new column that stores this information? So ideally it looks something like this:



      id age  friend1 friend2 AvgAgeF
      01 15 02 05 28
      02 23 01 05 24
      03 51 04 41
      04 41 03 51
      05 33 01 02 19


      Currently I can run the following code to do this:



      inx <- grep("friend", names(dat))
      tmp <- sapply(inx, function(i) dat$age[dat[[i]]])
      dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)


      However, when the "id" for the people aren't in order or aren't numbers at all such as in the following example:



      dat <- read.table(text = "
      id age friend1 friend2
      Bob 15 Jack Sam
      Jack 23 Sam Bob
      Sam 51 Bob Jack
      Sara 41 Henry NA
      Henry 33 Sara NA
      ", header = TRUE)


      The code appears to process incorrectly resulting in the following table (All but Sam's AvgAgeF is incorrect in this case):



           id age friend1 friend2 AvgAgeF
      1 Bob 15 Jack Sam 51
      2 Jack 23 Sam Bob 28
      3 Sam 51 Bob Jack 19
      4 Sara 41 Henry <NA> 23
      5 Henry 33 Sara <NA> 33


      How can I solve for this without changing the Names into numerical ids.










      share|improve this question
















      So I have a data frame like the following:



      id age  friend1 friend2
      01 15 02 05
      02 23 01 05
      03 51 04
      04 41 03
      05 33 01 02


      How can I calculate the average age of the friends and create a new column that stores this information? So ideally it looks something like this:



      id age  friend1 friend2 AvgAgeF
      01 15 02 05 28
      02 23 01 05 24
      03 51 04 41
      04 41 03 51
      05 33 01 02 19


      Currently I can run the following code to do this:



      inx <- grep("friend", names(dat))
      tmp <- sapply(inx, function(i) dat$age[dat[[i]]])
      dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)


      However, when the "id" for the people aren't in order or aren't numbers at all such as in the following example:



      dat <- read.table(text = "
      id age friend1 friend2
      Bob 15 Jack Sam
      Jack 23 Sam Bob
      Sam 51 Bob Jack
      Sara 41 Henry NA
      Henry 33 Sara NA
      ", header = TRUE)


      The code appears to process incorrectly resulting in the following table (All but Sam's AvgAgeF is incorrect in this case):



           id age friend1 friend2 AvgAgeF
      1 Bob 15 Jack Sam 51
      2 Jack 23 Sam Bob 28
      3 Sam 51 Bob Jack 19
      4 Sara 41 Henry <NA> 23
      5 Henry 33 Sara <NA> 33


      How can I solve for this without changing the Names into numerical ids.







      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 22:26







      Jack

















      asked Nov 21 '18 at 23:56









      JackJack

      164




      164
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Subset column age with friend1 and friend2 as indices, then cbind the resulting vectors. It's now a simple call to rowMeans.



          tmp <- with(dat, cbind(age[friend1], age[friend2]))
          dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)
          rm(tmp)

          dat
          # id age friend1 friend2 AvgAgeF
          #1 1 15 2 5 28
          #2 2 23 1 5 24
          #3 3 51 4 NA 41
          #4 4 41 3 NA 51
          #5 5 33 1 2 19


          Edit.



          If there are an indeterminate number of friend* columns, another strategy must be followed.

          First a dataset, starting with the question data.



          set.seed(1234)

          tmp <- matrix(sample(c(NA, 1:5), 20, TRUE), nrow = 5)
          colnames(tmp) <- paste0("friend", 3:6)
          dat <- cbind(dat, tmp)


          Now, use grep to get the friend columns.



          inx <- grep("friend", names(dat))
          tmp <- sapply(inx, function(i) dat$age[dat[[i]]])
          dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)


          Final cleanup.



          rm(tmp)


          Edit 2 - id are character strings.



          If the columns id and friend* are character strings instead of numeric, index column age with match.



          inx <- grep("friend", names(dat2))
          tmp <- sapply(inx, function(i) {
          x <- as.character(dat2[[i]])
          y <- as.character(dat2$id)
          dat2$age[match(x, y)]
          })
          dat2$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)
          rm(tmp)

          dat2
          # id age friend1 friend2 AvgAgeF
          #1 Bob 15 Jack Sam 37
          #2 Jack 23 Sam Bob 33
          #3 Sam 51 Bob Jack 19
          #4 Sara 41 Henry <NA> 33
          #5 Henry 33 Sara <NA> 41


          Data.



          dat <- read.table(text = "
          id age friend1 friend2
          01 15 02 05
          02 23 01 05
          03 51 04 NA
          04 41 03 NA
          05 33 01 02
          ", header = TRUE)

          dat2 <- read.table(text = "
          id age friend1 friend2
          Bob 15 Jack Sam
          Jack 23 Sam Bob
          Sam 51 Bob Jack
          Sara 41 Henry NA
          Henry 33 Sara NA
          ", header = TRUE)





          share|improve this answer


























          • Is there an easier way to do this if I had, let's say like 20 "friend" columns and I didn't want to do it manually?

            – Jack
            Nov 22 '18 at 19:04











          • @Jack Done, see the edit.

            – Rui Barradas
            Nov 22 '18 at 20:33











          • I was using this code and I realized that if the id is numerically " out of place " per say, that it ceases to work properly. The error I got was when I replaced the id 05 with 06 and all instances of it with the id 06. When this happens, the code treats the id as if it is a NA. Is there a way to go around this problem? I realize that I can just convert all the ids so that there isn't this problem but in the data I am handling I cannot do this since the id contains other information.

            – Jack
            Nov 23 '18 at 20:19













          • @Jack I don't understand, the code only processes the other columns, why is the id relevant? Can you edit the question with an example?

            – Rui Barradas
            Nov 23 '18 at 22:10











          • I edited the post. So in this case the id's/identifiers are no longer numerical and when this happens the code doesn't run correctly.

            – Jack
            Nov 23 '18 at 22:30











          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%2f53422093%2fextracting-and-using-information-from-other-rows-in-a-dataframe%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














          Subset column age with friend1 and friend2 as indices, then cbind the resulting vectors. It's now a simple call to rowMeans.



          tmp <- with(dat, cbind(age[friend1], age[friend2]))
          dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)
          rm(tmp)

          dat
          # id age friend1 friend2 AvgAgeF
          #1 1 15 2 5 28
          #2 2 23 1 5 24
          #3 3 51 4 NA 41
          #4 4 41 3 NA 51
          #5 5 33 1 2 19


          Edit.



          If there are an indeterminate number of friend* columns, another strategy must be followed.

          First a dataset, starting with the question data.



          set.seed(1234)

          tmp <- matrix(sample(c(NA, 1:5), 20, TRUE), nrow = 5)
          colnames(tmp) <- paste0("friend", 3:6)
          dat <- cbind(dat, tmp)


          Now, use grep to get the friend columns.



          inx <- grep("friend", names(dat))
          tmp <- sapply(inx, function(i) dat$age[dat[[i]]])
          dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)


          Final cleanup.



          rm(tmp)


          Edit 2 - id are character strings.



          If the columns id and friend* are character strings instead of numeric, index column age with match.



          inx <- grep("friend", names(dat2))
          tmp <- sapply(inx, function(i) {
          x <- as.character(dat2[[i]])
          y <- as.character(dat2$id)
          dat2$age[match(x, y)]
          })
          dat2$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)
          rm(tmp)

          dat2
          # id age friend1 friend2 AvgAgeF
          #1 Bob 15 Jack Sam 37
          #2 Jack 23 Sam Bob 33
          #3 Sam 51 Bob Jack 19
          #4 Sara 41 Henry <NA> 33
          #5 Henry 33 Sara <NA> 41


          Data.



          dat <- read.table(text = "
          id age friend1 friend2
          01 15 02 05
          02 23 01 05
          03 51 04 NA
          04 41 03 NA
          05 33 01 02
          ", header = TRUE)

          dat2 <- read.table(text = "
          id age friend1 friend2
          Bob 15 Jack Sam
          Jack 23 Sam Bob
          Sam 51 Bob Jack
          Sara 41 Henry NA
          Henry 33 Sara NA
          ", header = TRUE)





          share|improve this answer


























          • Is there an easier way to do this if I had, let's say like 20 "friend" columns and I didn't want to do it manually?

            – Jack
            Nov 22 '18 at 19:04











          • @Jack Done, see the edit.

            – Rui Barradas
            Nov 22 '18 at 20:33











          • I was using this code and I realized that if the id is numerically " out of place " per say, that it ceases to work properly. The error I got was when I replaced the id 05 with 06 and all instances of it with the id 06. When this happens, the code treats the id as if it is a NA. Is there a way to go around this problem? I realize that I can just convert all the ids so that there isn't this problem but in the data I am handling I cannot do this since the id contains other information.

            – Jack
            Nov 23 '18 at 20:19













          • @Jack I don't understand, the code only processes the other columns, why is the id relevant? Can you edit the question with an example?

            – Rui Barradas
            Nov 23 '18 at 22:10











          • I edited the post. So in this case the id's/identifiers are no longer numerical and when this happens the code doesn't run correctly.

            – Jack
            Nov 23 '18 at 22:30
















          1














          Subset column age with friend1 and friend2 as indices, then cbind the resulting vectors. It's now a simple call to rowMeans.



          tmp <- with(dat, cbind(age[friend1], age[friend2]))
          dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)
          rm(tmp)

          dat
          # id age friend1 friend2 AvgAgeF
          #1 1 15 2 5 28
          #2 2 23 1 5 24
          #3 3 51 4 NA 41
          #4 4 41 3 NA 51
          #5 5 33 1 2 19


          Edit.



          If there are an indeterminate number of friend* columns, another strategy must be followed.

          First a dataset, starting with the question data.



          set.seed(1234)

          tmp <- matrix(sample(c(NA, 1:5), 20, TRUE), nrow = 5)
          colnames(tmp) <- paste0("friend", 3:6)
          dat <- cbind(dat, tmp)


          Now, use grep to get the friend columns.



          inx <- grep("friend", names(dat))
          tmp <- sapply(inx, function(i) dat$age[dat[[i]]])
          dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)


          Final cleanup.



          rm(tmp)


          Edit 2 - id are character strings.



          If the columns id and friend* are character strings instead of numeric, index column age with match.



          inx <- grep("friend", names(dat2))
          tmp <- sapply(inx, function(i) {
          x <- as.character(dat2[[i]])
          y <- as.character(dat2$id)
          dat2$age[match(x, y)]
          })
          dat2$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)
          rm(tmp)

          dat2
          # id age friend1 friend2 AvgAgeF
          #1 Bob 15 Jack Sam 37
          #2 Jack 23 Sam Bob 33
          #3 Sam 51 Bob Jack 19
          #4 Sara 41 Henry <NA> 33
          #5 Henry 33 Sara <NA> 41


          Data.



          dat <- read.table(text = "
          id age friend1 friend2
          01 15 02 05
          02 23 01 05
          03 51 04 NA
          04 41 03 NA
          05 33 01 02
          ", header = TRUE)

          dat2 <- read.table(text = "
          id age friend1 friend2
          Bob 15 Jack Sam
          Jack 23 Sam Bob
          Sam 51 Bob Jack
          Sara 41 Henry NA
          Henry 33 Sara NA
          ", header = TRUE)





          share|improve this answer


























          • Is there an easier way to do this if I had, let's say like 20 "friend" columns and I didn't want to do it manually?

            – Jack
            Nov 22 '18 at 19:04











          • @Jack Done, see the edit.

            – Rui Barradas
            Nov 22 '18 at 20:33











          • I was using this code and I realized that if the id is numerically " out of place " per say, that it ceases to work properly. The error I got was when I replaced the id 05 with 06 and all instances of it with the id 06. When this happens, the code treats the id as if it is a NA. Is there a way to go around this problem? I realize that I can just convert all the ids so that there isn't this problem but in the data I am handling I cannot do this since the id contains other information.

            – Jack
            Nov 23 '18 at 20:19













          • @Jack I don't understand, the code only processes the other columns, why is the id relevant? Can you edit the question with an example?

            – Rui Barradas
            Nov 23 '18 at 22:10











          • I edited the post. So in this case the id's/identifiers are no longer numerical and when this happens the code doesn't run correctly.

            – Jack
            Nov 23 '18 at 22:30














          1












          1








          1







          Subset column age with friend1 and friend2 as indices, then cbind the resulting vectors. It's now a simple call to rowMeans.



          tmp <- with(dat, cbind(age[friend1], age[friend2]))
          dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)
          rm(tmp)

          dat
          # id age friend1 friend2 AvgAgeF
          #1 1 15 2 5 28
          #2 2 23 1 5 24
          #3 3 51 4 NA 41
          #4 4 41 3 NA 51
          #5 5 33 1 2 19


          Edit.



          If there are an indeterminate number of friend* columns, another strategy must be followed.

          First a dataset, starting with the question data.



          set.seed(1234)

          tmp <- matrix(sample(c(NA, 1:5), 20, TRUE), nrow = 5)
          colnames(tmp) <- paste0("friend", 3:6)
          dat <- cbind(dat, tmp)


          Now, use grep to get the friend columns.



          inx <- grep("friend", names(dat))
          tmp <- sapply(inx, function(i) dat$age[dat[[i]]])
          dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)


          Final cleanup.



          rm(tmp)


          Edit 2 - id are character strings.



          If the columns id and friend* are character strings instead of numeric, index column age with match.



          inx <- grep("friend", names(dat2))
          tmp <- sapply(inx, function(i) {
          x <- as.character(dat2[[i]])
          y <- as.character(dat2$id)
          dat2$age[match(x, y)]
          })
          dat2$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)
          rm(tmp)

          dat2
          # id age friend1 friend2 AvgAgeF
          #1 Bob 15 Jack Sam 37
          #2 Jack 23 Sam Bob 33
          #3 Sam 51 Bob Jack 19
          #4 Sara 41 Henry <NA> 33
          #5 Henry 33 Sara <NA> 41


          Data.



          dat <- read.table(text = "
          id age friend1 friend2
          01 15 02 05
          02 23 01 05
          03 51 04 NA
          04 41 03 NA
          05 33 01 02
          ", header = TRUE)

          dat2 <- read.table(text = "
          id age friend1 friend2
          Bob 15 Jack Sam
          Jack 23 Sam Bob
          Sam 51 Bob Jack
          Sara 41 Henry NA
          Henry 33 Sara NA
          ", header = TRUE)





          share|improve this answer















          Subset column age with friend1 and friend2 as indices, then cbind the resulting vectors. It's now a simple call to rowMeans.



          tmp <- with(dat, cbind(age[friend1], age[friend2]))
          dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)
          rm(tmp)

          dat
          # id age friend1 friend2 AvgAgeF
          #1 1 15 2 5 28
          #2 2 23 1 5 24
          #3 3 51 4 NA 41
          #4 4 41 3 NA 51
          #5 5 33 1 2 19


          Edit.



          If there are an indeterminate number of friend* columns, another strategy must be followed.

          First a dataset, starting with the question data.



          set.seed(1234)

          tmp <- matrix(sample(c(NA, 1:5), 20, TRUE), nrow = 5)
          colnames(tmp) <- paste0("friend", 3:6)
          dat <- cbind(dat, tmp)


          Now, use grep to get the friend columns.



          inx <- grep("friend", names(dat))
          tmp <- sapply(inx, function(i) dat$age[dat[[i]]])
          dat$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)


          Final cleanup.



          rm(tmp)


          Edit 2 - id are character strings.



          If the columns id and friend* are character strings instead of numeric, index column age with match.



          inx <- grep("friend", names(dat2))
          tmp <- sapply(inx, function(i) {
          x <- as.character(dat2[[i]])
          y <- as.character(dat2$id)
          dat2$age[match(x, y)]
          })
          dat2$AvgAgeF <- rowMeans(tmp, na.rm = TRUE)
          rm(tmp)

          dat2
          # id age friend1 friend2 AvgAgeF
          #1 Bob 15 Jack Sam 37
          #2 Jack 23 Sam Bob 33
          #3 Sam 51 Bob Jack 19
          #4 Sara 41 Henry <NA> 33
          #5 Henry 33 Sara <NA> 41


          Data.



          dat <- read.table(text = "
          id age friend1 friend2
          01 15 02 05
          02 23 01 05
          03 51 04 NA
          04 41 03 NA
          05 33 01 02
          ", header = TRUE)

          dat2 <- read.table(text = "
          id age friend1 friend2
          Bob 15 Jack Sam
          Jack 23 Sam Bob
          Sam 51 Bob Jack
          Sara 41 Henry NA
          Henry 33 Sara NA
          ", header = TRUE)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 11:40

























          answered Nov 22 '18 at 0:26









          Rui BarradasRui Barradas

          17k51730




          17k51730













          • Is there an easier way to do this if I had, let's say like 20 "friend" columns and I didn't want to do it manually?

            – Jack
            Nov 22 '18 at 19:04











          • @Jack Done, see the edit.

            – Rui Barradas
            Nov 22 '18 at 20:33











          • I was using this code and I realized that if the id is numerically " out of place " per say, that it ceases to work properly. The error I got was when I replaced the id 05 with 06 and all instances of it with the id 06. When this happens, the code treats the id as if it is a NA. Is there a way to go around this problem? I realize that I can just convert all the ids so that there isn't this problem but in the data I am handling I cannot do this since the id contains other information.

            – Jack
            Nov 23 '18 at 20:19













          • @Jack I don't understand, the code only processes the other columns, why is the id relevant? Can you edit the question with an example?

            – Rui Barradas
            Nov 23 '18 at 22:10











          • I edited the post. So in this case the id's/identifiers are no longer numerical and when this happens the code doesn't run correctly.

            – Jack
            Nov 23 '18 at 22:30



















          • Is there an easier way to do this if I had, let's say like 20 "friend" columns and I didn't want to do it manually?

            – Jack
            Nov 22 '18 at 19:04











          • @Jack Done, see the edit.

            – Rui Barradas
            Nov 22 '18 at 20:33











          • I was using this code and I realized that if the id is numerically " out of place " per say, that it ceases to work properly. The error I got was when I replaced the id 05 with 06 and all instances of it with the id 06. When this happens, the code treats the id as if it is a NA. Is there a way to go around this problem? I realize that I can just convert all the ids so that there isn't this problem but in the data I am handling I cannot do this since the id contains other information.

            – Jack
            Nov 23 '18 at 20:19













          • @Jack I don't understand, the code only processes the other columns, why is the id relevant? Can you edit the question with an example?

            – Rui Barradas
            Nov 23 '18 at 22:10











          • I edited the post. So in this case the id's/identifiers are no longer numerical and when this happens the code doesn't run correctly.

            – Jack
            Nov 23 '18 at 22:30

















          Is there an easier way to do this if I had, let's say like 20 "friend" columns and I didn't want to do it manually?

          – Jack
          Nov 22 '18 at 19:04





          Is there an easier way to do this if I had, let's say like 20 "friend" columns and I didn't want to do it manually?

          – Jack
          Nov 22 '18 at 19:04













          @Jack Done, see the edit.

          – Rui Barradas
          Nov 22 '18 at 20:33





          @Jack Done, see the edit.

          – Rui Barradas
          Nov 22 '18 at 20:33













          I was using this code and I realized that if the id is numerically " out of place " per say, that it ceases to work properly. The error I got was when I replaced the id 05 with 06 and all instances of it with the id 06. When this happens, the code treats the id as if it is a NA. Is there a way to go around this problem? I realize that I can just convert all the ids so that there isn't this problem but in the data I am handling I cannot do this since the id contains other information.

          – Jack
          Nov 23 '18 at 20:19







          I was using this code and I realized that if the id is numerically " out of place " per say, that it ceases to work properly. The error I got was when I replaced the id 05 with 06 and all instances of it with the id 06. When this happens, the code treats the id as if it is a NA. Is there a way to go around this problem? I realize that I can just convert all the ids so that there isn't this problem but in the data I am handling I cannot do this since the id contains other information.

          – Jack
          Nov 23 '18 at 20:19















          @Jack I don't understand, the code only processes the other columns, why is the id relevant? Can you edit the question with an example?

          – Rui Barradas
          Nov 23 '18 at 22:10





          @Jack I don't understand, the code only processes the other columns, why is the id relevant? Can you edit the question with an example?

          – Rui Barradas
          Nov 23 '18 at 22:10













          I edited the post. So in this case the id's/identifiers are no longer numerical and when this happens the code doesn't run correctly.

          – Jack
          Nov 23 '18 at 22:30





          I edited the post. So in this case the id's/identifiers are no longer numerical and when this happens the code doesn't run correctly.

          – Jack
          Nov 23 '18 at 22:30




















          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%2f53422093%2fextracting-and-using-information-from-other-rows-in-a-dataframe%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

          Npm cannot find a required file even through it is in the searched directory