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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:21:34+00:00 2026-06-06T09:21:34+00:00

I have some values in a dataframe called ‘values’ ie: Sam Ned Ted Ann

  • 0

I have some values in a dataframe called ‘values’

ie:

Sam Ned Ted Ann
500 430 57  212
410 265 69  341
189 554 153 457
236 311 590 271
50  568 234 442

And some quality control flags in a separate dataframe ‘flags’

Sam$F   Ned$F   Ted$F   Ann$F
1   1   1   0 
0   0   1   0
0   0   1   0
0   0   0   0
0   0   1   0

I would like to replace the number in ‘values’ with NA if the equivalent entry in flags is a 1 i.e. resulting in the following

Sam Ned Ted Ann
NA  NA  NA  212
410 265 NA  341
189 554 NA  457
236 311 590 271
50  568 NA  442

This question is very similar to this one: Replacing certain values in data.frame in R

Which has been solved neatly with a ‘merge tables’ solution.

Except that I have a lot of data columns to perform this on, not just one. However I should be able to get the same ‘merge tables’ solution to work for me.

To merge the 2 tables (and replace 1s with NA I have used the following

F2 <-Flags
F2[F2 == "1"] <- "NA"
# Create identical column names for values and F2 for matching
samples <-colnames(Rvalues)
colnames(F2) <-samples
#Create an ID column for F2 and values
F2$ID <- c(1,2,3,4,5)
Rvalues$ID <- c(1,2,3,4,5)
out2 <- merge(Rvalues, F2, by = c("ID"), all.x = TRUE)

The resulting out2 dataframe is as follows:

ID  Sam.x   Ned.x   Ted.x   Ann.x   Sam.y   Ned.y   Ted.y   Ann.y 
1   1   500 430 57  212 NA  NA  NA  0
2   2   410 265 69  341 0   0   NA  0
3   3   189 554 153 457 0   0   NA  0
4   4   236 311 590 271 0   0   0   0
5   5   50  568 234 442 0   0   NA  0

So I would like to use the solution posted (by Chase) in the link above, within a loop to go through the columns for each sample.

I have tried the following:

for [i in samples]
{
out3 <- transform(out2, [i]$new = ifelse(is.na([i].x), [i].y, [i].x), [i].x = NULL, [i].y = NULL)}

It is not working (error message

"Error: unexpected '[' in:
"{
out3 <- transform(out2, newdata = ifelse(is.na(["")

So I have something wrong, without the [] brackets it also does not work.

Any fixes greatly appreciated, many thanks

  • 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-06T09:21:36+00:00Added an answer on June 6, 2026 at 9:21 am

    If your ‘values’ and ‘flags’ data.frames have the same dimension and order of columns

    values[flags == 1] <- NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with a grouping variable 'ID' and some values ('Value'):
I have a data frame where some of the columns contain NA values. How
I have some values in a configuration file (XML file) with some values like
In my C++ application, I have some values that act as codes to represent
I have some financial values stored as text in a mysql db. the significance
I have some double values I want to convert to a string with this
I have some code that sets up a dictionary with some defualt values for
I have to transfer some values to be used as commands over the network,
Introduction I have some sort of values that I might want to access several
hi I wanted to have some keys and values filtered out when compared with

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.