Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7170473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:13:14+00:00 2026-05-28T15:13:14+00:00

I have two tables, and I’m trying to look up values from one to

  • 0

I have two tables, and I’m trying to look up values from one to add to values in the other. Currently I am using two for loops but they run slowly. I am new to R and know I should avoid looping to speed things up but I can’t figure out how.

Table1 (several thousand rows, 37 columns):

type cat1 cat2 cat3 ... cat36
   1    2    3    2         7
   3    6    2    1         9
   2    4    6    7         4
   3    5    7    8         2
   5    2    2    9         1
   4    3    1    2         3
   1    8    1    4         4
...

Table2 (36 rows, 5 columns):

      type1 type2 type3 type4 type5
cat1      2     3     4     3     8
cat2      8     5     5     2     6
cat3      7     5     1     3     5
...
cat36     4     7     2     8     9

I want to modify each value in Table1 by adding the appropriate value (matching among the 5 types and 36 categories) from Table2. Here are the desired results:

type cat1 cat2 cat3 ... cat36
   1    4   11    9        11
   3   10    7    2        11
   2    7   11   12        11
   3    9   12    9         4
   5   10    8   14        10
   4    6    3    5        11
   1   10    9   11         8
...

Here is my current (slow) code:

for (i in 1:36) {
    for (j in 1:nrow(Table1)) {
        Table1[j,i+1] = Table1[j,i+1] + Table2[i,Table1[j,1]]
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:13:15+00:00Added an answer on May 28, 2026 at 3:13 pm

    The type column in Table1 indicates which column in Table2 to add to the rows in Table1. So use the “type” column as an index to the Table2 rows, and then transpose the resulting matrix so you can add rows to rows:

    Table3 <- cbind(Table1[ , "type"], 
                    t(Table2[ , Table1[ , "type"] ]) + Table1[ , -1])
    

    (I’m assuming that Table1 and Table2 are matrices. If they are data frames you can use Table1$type instead of Table1[,"type"]).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have two sets of data (two tables) for patient records, one 1999-2003, the other
I have two tables, one linked to the Primary Key of the other. At
I have two tables. One contains members and the other members dogs . The
I have two tables USER and TRANSACTION i want to choose the data from
I have two tables table1 , table2 . Both have a one to one
I have two tables inside a database. One stores unique userNames and a unique
I have two tables with this structure: Table one: ID Description Table two: ID
I have two tables. One table contains information about Assets, another Table about their
I have two tables. One is temp table and another is main table. In
I have two tables which look like the following tools : id | part

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.