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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:02:07+00:00 2026-05-31T15:02:07+00:00

I have a dataframe df: colour shape ‘red’ circle ‘blue’ square ‘blue’ circle ‘green’

  • 0

I have a dataframe df:

colour  shape
'red'   circle
'blue'  square
'blue'  circle
'green' sphere

And a double matrix m with named rows/columns

      circle square sphere  
red   1      4      7
blue  2      5      8
green 3      6      9

I’d like to add a new column to DF such that I obtain:

id  colour  shape
1   'red'   circle
5   'blue'  square
2   'blue'  circle
9   'green' sphere

I’ve tried doing this with the following code but it doesn’t seem to work:

df$id <- m[df$colour,df$shape]

I’ve also tried apply(); and similar but with no luck. Can anyone tell me the right approach to doing this without using a loop?

  • 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-31T15:02:08+00:00Added an answer on May 31, 2026 at 3:02 pm

    I think I might win the shortest answer contest here as long as those are character vectors rather than factors which might be more expected unless you made specifid effort to avoid. It really only adds cbind to convert the two df “character” vectors to a two column matrix expected by the [.matrix function that you were very close to success in using. (And it also seems reasonably expressive.)

    # Data construct
    d <- data.frame(color=c('red','blue','blue','green'), 
    shape=c('circle','square','circle','sphere'), stringsAsFactors=FALSE)
     m <- matrix(1:9, 3,3, dimnames=list(c('red','blue','green'), c('circle','square','sphere')))
    # Code:
    
     d$id <- with( d, m [ cbind(color, shape) ] )
     d
      color  shape id
    1   red circle  1
    2  blue square  5
    3  blue circle  2
    4 green sphere  9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a dataframe with a column of integers that I would like to
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
(second question today - must be a bad day) I have a dataframe with
I have a dataframe with 10 rows df <- c(1:10) How do I add
I have a dataframe with 3 columns $x -- at http://pastebin.com/SGrRUJcA $y -- at
I have a dataframe with 6 columns and many rows that includes positions for
I have a dataframe that looks like this DF: V1 V2 V3 V4 V5
I have a input dataframe like this (the real one is very large, so
I have a dataframe. first column contains my the predictive score (range from 0

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.