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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:09:16+00:00 2026-06-14T07:09:16+00:00

I have the following code: set.seed(47) df <- data.frame(V1 = sample(letters[1:5], size = 10,

  • 0

I have the following code:

set.seed(47)
df <- data.frame(V1 = sample(letters[1:5], size = 10, replace = TRUE),
                 V2 = 101:110)

partial_key <- data.frame(V1 = letters[1:3], V2 = 1:3)

> df
   V1  V2
1   e 101
2   b 102
3   d 103
4   e 104
5   c 105
6   d 106
7   b 107
8   c 108
9   c 109
10  e 110


> partial_key
  V1 V2
1  a  1
2  b  2
3  c  3

I’d like to replace the values of V2 in df with the corresponding values from partial_key that match in the V1 columns. The non-matches should remain as is.

With a complete key, I’d use match, which replaces the correct values, but replaces the non-matches with NA.

df[, "V2"] <- partial_key[match(df$V1, partial_key$V1), "V2"]
## Replaces too much

I can hack together a solution with %in%, but is there a better way? Something more intuitive, with less typing?

df[df$V1 %in% partial_key$V1, "V2"] <-
partial_key[match(df$V1[df$V1 %in% partial_key$V1], partial_key$V1), "V2"]
## Works, but is there a better way?
> df
   V1  V2
1   e 101
2   b   2
3   d 103
4   e 104
5   c   3
6   d 106
7   b   2
8   c   3
9   c   3
10  e 110
  • 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-14T07:09:17+00:00Added an answer on June 14, 2026 at 7:09 am

    Using %in% is unnecessary since the output of match already contains that info. So you can do something like this:

    replacement <- partial_key$V2[match(df$V1, partial_key$V1)]
    df$V2 <- ifelse(is.na(replacement), df$V2, replacement)
    

    Sometimes I wish R had a base if.na function similar to Excel’s IFERROR. I have it in my Rprofile:

    if.na <- function(value, value.if.na) ifelse(is.na(value), value.if.na, value)
    df$V2 <- if.na(replacement, df$V2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I produced multiple plots using the following code: set.seed(12345) a <- data.frame(Glabel=LETTERS[1:7], A=rnorm(7, mean
I have the following drawing code: [[NSColor redColor] set]; NSRect fillRect = NSMakeRect(bounds.size.width -
I have the following code: Set myMailanon = CreateObject(CDO.Message) myMailanon.MimeFormatted = True Set myConfanon
I'm using socket.io and express and have the following code: io.set('authorization', function (data, accept)
I have the following code set up in my Startup IDictionary<string, string> properties =
I have the following code to set a userId variable: (userId set in prior
I have following code in my application: // to set tip - photo in
I have the following code: UPDATE myTable SET Col1 = @Value However, I have
I have used the following code to set cookie and then redirect. String level=(String)
I have following code. ASPX Page <a href=AnyASPXPageOfWebsite.aspx onclick=javascript:CallJQuery(); > Set Price </a> JS

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.