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 8964809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:40:25+00:00 2026-06-15T16:40:25+00:00

I am relatively new with R and I have a problem with a dataframe.

  • 0

I am relatively new with R and I have a problem with a dataframe.
I have a very long dataframe (df1) with some coordinates xy and a value z. I have a shorter dataframe (df2) with the same columns but smaller number of rows. I want to replace values in df1 when xy are equal in df2.

x = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4) 
y = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
z = c(8, 5, 3, 1, 2, 6, 8, 5, 3, 2, 8, 4, 4, 6, 2, 1) 
df1 = data.frame(x, y, z)      

x1=c(1,3,4)
y1=c(2,1,4)
z1=c(58,37,23)

df2=data.frame(x1,y1,z1)
names(df2) <- c("x", "y", "z")

I thought that I might use ifelse function as:

df1$znew<-ifelse((df1[,1]== df2[,1])&(df1[,2]==df2[,2]), df2[,3], df1[,3])

But the two objects are not the same dimensions.

I have tried to use loops so it analyse each row to compare x and y and then decide what z to use but I can’t make it work.

At the end I would like to have a dataframe with a new variable of z to compare the values and corroborate that it really changed the values. My final dataframe would look like:

 znew = c(8,58,3,1,2,6,8,5,37,2,8,4,4,6,2,23)

I really appreciate any help and I am sorry if somebody else posted similar questions, I have been all day trying to figure it out and I can’t find any example that suits my case.

  • 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-15T16:40:26+00:00Added an answer on June 15, 2026 at 4:40 pm

    Assuming the two data frames do in fact have the same column names (probably just a typo in your question), you might do this with merge:

    tmp <- merge(df1,df2,all.x = TRUE,by = c('x','y'))
    tmp$z.x[!is.na(tmp$z.y)] <- tmp$z.y[!is.na(tmp$z.y)]
    > tmp
       x y z.x z.y
    1  1 1   8  NA
    2  1 2   4   4
    3  1 3   3  NA
    4  1 4   1  NA
    5  2 1   2  NA
    6  2 2   6  NA
    7  2 3   8  NA
    8  2 4   5  NA
    9  3 1   4   4
    10 3 2   2  NA
    11 3 3   8  NA
    12 3 4   4  NA
    13 4 1   4  NA
    14 4 2   6  NA
    15 4 3   2  NA
    16 4 4   3   3
    

    Then just remove the extra column and rename the columns.

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

Sidebar

Related Questions

I'm relatively new to c++, and have some issues with ifstream. All I want
I am relatively new to using jQuery and I have a problem with creating
I'm relatively new in the Java world and I have a problem which I
I'm relatively new at PHP and came across a slight problem. I have a
I am relatively new to xslt, but I have had some luck styling a
I'm relatively new to gwt and I have a problem using JsArrayInteger. Here is
I am relatively new to PHP/MySQL and have been having a problem that I
I have the following (correct) solution to Project Euler problem 24. I'm relatively new
I am relatively new to WPF and I have run into a little problem.
I am relatively new to JMS and have encountered a weird problem implementing my

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.