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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:55:33+00:00 2026-06-13T00:55:33+00:00

This problem has plagued me for quite some time. I always just work around

  • 0

This problem has plagued me for quite some time. I always just work around it with a for loop, but I think it’s finally time for me to find a quicker and more elegant way of doing this.

As an example, let’s say I have a data frame containing information on whether an item is red or blue. The information is presented in this way:

item.df <- data.frame(Item=seq(1,5), Red=c("Y", "Y", "N", "N", "N"), Blue=c("N", "N", "Y", "Y", "N"))

Clearly, this is not the most condensed way to represent this information. Instead of having two separate Red and Blue columns, I simply want one item color column that would contain “Red”, “Blue”, or “Neither” (Or NA would also be acceptable).

Obviously, I can achieve this by creating an empty Item.Color column and then filling it in by looping through each individual row. But I’m sure there is a quicker way to do this.

Back when I was a true R novice, I tried to do it by:

item.df$Item.Color <- if(item.df$Red=="Y"){"Red"}

but I quickly learned this doesn’t work, because the if statement will only read the first element in item.df$Red.

Could there be a way to achieve this using do.call() or one of the apply() functions? I’ve attempted, but I could never get it to do quite what I wanted. Thanks in advance for any insight you might be able to provide!

p.s. I would also be grateful to hear any suggestions for a better title for this question. For me, that always seems to be the hardest part in asking questions.

  • 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-13T00:55:34+00:00Added an answer on June 13, 2026 at 12:55 am

    The following code should do the trick, it even checks if the data contains rows where both Red and Blue are TRUE (== "Y").

    item.df[["condensed"]] <- NA
    item.df = within(item.df, {
      if(any(Red == "Y" & Blue == "Y")) stop("Blue and red cannot both be TRUE")
      condensed[Red == "Y"] <- "Red"
      condensed[Blue == "Y"] <- "Blue"
    })
    

    The trick here is that the same syntax for taking a subset can be used for assignment:

    # Getting a subset from a vector
    A[B > 2]
    # Or assigning NA to that subset 
    A[B > 2] <- NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem has been solved before, but I'm just not getting it with examples
This problem has been plaguing me for a little while, but I've think I've
This problem has been solved before, but I'm just not getting it with examples
This problem has been bugging me for a long time. For example, the code
Premise This problem has a known solution (shown below actually), I'm just wondering if
By some coincidence this problem has come up twice in the last week. A
you will think: "This problem has been solved many, many times. So why doesn't
This problem has bothered me for a while and Im sure theres some sort
This problem has been afflicting me for quite a while and it's been really
I'm very sure this problem has been solved, but I can't find any information

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.