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

  • Home
  • SEARCH
  • 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 7827061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:42:21+00:00 2026-06-02T09:42:21+00:00

I have two data frames like this: df.1 <- data.frame( var.1 = sample(1:10), code

  • 0

I have two data frames like this:

df.1 <- data.frame(
     var.1 = sample(1:10),
     code = sample(c("A", "B", "C"), 10, replace = TRUE))

df.2 <- data.frame(
    var.2 = sample(1:3),
    row.names=c("A","B","C"))

What I need to do is to add a third column df.1$var.2 which, for each value in df.1$code take the value from df.2$var.2 accordingly to their row name.

I got to this point but with no success.. Suggestions?

for (i in 1:length(df.1$code)){
    if(df.1$code[i] == rownames(df.2))
    df.1$var.2[i] <- df.2$var.2
    }
  • 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-06-02T09:42:22+00:00Added an answer on June 2, 2026 at 9:42 am

    You mean like this:

    df.2$code <- rownames(df.2)
    > merge(df.1,df.2,by = "code")
       code var.1 var.2
    1     A     5     1
    2     B     3     2
    3     B     2     2
    4     B     7     2
    5     B    10     2
    6     C     8     3
    7     C     4     3
    8     C     1     3
    9     C     9     3
    10    C     6     3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two data.frames. For examples sake let's say they look like this: df1
i have data frame like this : 1 2 3 4 5 1 0.1
Say I have a data frame like this: Df <- data.frame( V1 = c(1,2,3,NA,5),
I have two dataframes, much like these: data = data.frame(data=cbind(1:12,rep(c(1,2),6),rep(c(1,2,3),4))) colnames(data)=c('v','h','c') lookup = data.frame(data=cbind(c(rep(1,3),rep(2,3)),rep(c(1,2,3),2),21:26))
I have two data frames like so: A B 1 6 2 7 5
I have two R data frame with differing dimensions. However but data frames have
I have a factored time series that looks like this: df <- data.frame(a=c(11-JUL-2004, 11-JUL-2005,
I have two data frames with two columns each. The first column is timestamps
I am using R and I have two data frames: carrots and cucumbers. Each
I have a data frame with two columns. First column contains categories such as

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.