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

The Archive Base Latest Questions

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

Given two data frames: C1<-c(3,4,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,6,3,7,4) DF<-data.frame(C1=C1,C2=C2,C3=C3) DF C1 C2 C3 1 3

  • 0

Given two data frames:

 C1<-c(3,4,4,4,5)
 C2<-c(3,7,3,4,5)
 C3<-c(5,6,3,7,4)
 DF<-data.frame(C1=C1,C2=C2,C3=C3)
 DF
   C1 C2 C3
1  3  3  5
2  4  7  6
3  4  3  3
4  4  4  7
5  5  5  4

and

V1<-c(3,2,2,4,5)
V2<-c(3,7,3,5,2)
V3<-c(5,2,5,7,5)
V4<-c(1,1,2,3,4)
V5<-c(1,2,6,7,5)
DF2<-data.frame(V1=V1,V2=V2,V3=V3,V4=V4,V5=V5)
DF2
  V1 V2 V3 V4 V5
1  3  3  5  1  1
2  2  7  2  1  2
3  2  3  5  2  6
4  4  5  7  3  7
5  5  2  5  4  5

Looking at each equivalent row in both data frames, there is a relationship between the value in C3 and the number of columns I want to drop in that same row in DF2.

The relationship between the value in C3 and the # of columns in DF2 to drop looks like this

If C3≥7 drop V5
If C3=6.0:6.9 drop V4 and up (so basically V5,V4)
If C3=5.0:5.9 drop V3 and up (so basically V5,V4,V3)
If C3=4.0:4.9 drop V2 and up (so basically V5,V4,V3,V2)
If C3≤3.9 drop entire row

For this example, based on the values of C3, I would want DF2 to look like this

  V1 V2 V3 V4 V5
1  3  3
2  2  7  2

4  4  5  7  3 
5  5

I’ve tried write a simple script to do this (I’m pretty new so I like to keep things simple so I can see what’s going on) but I’m throwing errors left and right so I’d appreciate some advice on how to proceed

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

    I like Koshke’s answer, but if your rules for setting to NA don’t have a nice mathematical property to them or you need to define your rules arbitrarily, this approach should give you that flexibility. First, define a function that returns the columns to drop based on your rules:

    f <- function(x) {
      if(x >= 7){
        out <- 5
      }else if(x >= 6.0){
               out <- 4:5
      } else if( x >= 5.0){
        out <- 3:5
      } else if (x >= 4.0){
        out <- 2:5
      } else {
        out <- 1:5
      }
      return(out)
    }
    

    Next, create a list for the column indices to drop:

    z <- lapply(DF$C3, f)
    

    Finally, loop through each row setting the corresponding columns to NA:

    for(j in seq(length(z))){
      DF2[j, z[[j]]] <- NA
    }
    
    #-----
      V1 V2 V3 V4 V5
    1  3  3 NA NA NA
    2  2  7  2 NA NA
    3 NA NA NA NA NA
    4  4  5  7  3 NA
    5  5 NA NA NA NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two data frames: df1 = data.frame(CustomerId = c(1:6), Product = c(rep(Toaster, 3), rep(Radio,
I have a list L of data frames with two columns each...a key, and
Just learning R. Given a data.frame in R with two columns, one numeric and
I have a data frame with two columns: an ID and some numeric value
I have two data.frames each with three columns: chrom, start & stop, let's call
I need to perform data validation of two tables using Selenium . Given a
I need query to get data between two days.I tried with this.But it gives
Given two arrays, how to find the maximum element which is common to both
I have two small data frames, this_tx and last_tx . They are, in every
R: how can I populate the rows of a data frame, in which each

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.