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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:35:51+00:00 2026-05-23T16:35:51+00:00

I know how to delete columns in R, but I am not sure how

  • 0

I know how to delete columns in R, but I am not sure how to delete them based on the following set of conditions.
Suppose a data frame such as:

DF <- data.frame(L = c(2,4,5,1,NA,4,5,6,4,3), J= c(3,4,5,6,NA,3,6,4,3,6), K= c(0,1,1,0,NA,1,1,1,1,1),D = c(1,1,1,1,NA,1,1,1,1,1))
 DF
   L  J  K  D
1  2  3  0  1
2  4  4  1  1
3  5  5  1  1
4  1  6  0  1
5 NA NA NA NA
6  4  3  1  1
7  5  6  1  1
8  6  4  1  1
9  4  3  1  1
10 3  6  1  1

The data frame has to be set up in this fashion. Column K corresponds to column L, and column D, corresponds to column J. Because column D has values that are all equal to one, I would like to delete column D, and the corresponding column J yielding a dataframe that looks like:

 DF
    L  K
1   2  0
2   4  1
3   5  1
4   1  0
5  NA NA
6   4  1
7   5  1
8   6  1
9   4  1
10  3  1

I know there has got to be a simple command to do so, I just can’t think of any. And if it makes any difference, the NA’s must be retained.

Additional helpful information, in my real data frame there are a total of 20 columns, so there are 10 columns like L and J, and another 10 that are like K and D, I need a function that can recognize the correspondence between these two groups and delete columns accordingly if necessary

Thank you in advance!

  • 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-23T16:35:51+00:00Added an answer on May 23, 2026 at 4:35 pm

    Okey, assuming the column-number based correspondence, here is an example:

    > n <- 10
    > 
    > # sample data
    > d <- data.frame(lapply(1:n, function(x)sample(n)), lapply(1:n, function(x)sample(2, n, T, c(0.1, 0.9))-1))
    > names(d) <- c(LETTERS[1:n], letters[1:n])
    > head(d)
       A B  C D E  F  G H  I  J a b c d e f g h i j
    1  5 5  2 7 4  3  4 3  5  8 0 1 1 1 1 1 1 1 1 1
    2  9 8  4 6 7  8  8 2 10  5 1 1 1 1 1 1 1 1 1 1
    3  6 6 10 3 5  6  2 1  8  6 1 1 1 1 1 1 1 1 1 1
    4  1 7  5 5 1 10 10 4  2  4 1 1 1 1 1 1 1 1 1 1
    5 10 9  6 2 9  5  6 9  9  9 1 1 0 1 1 1 1 1 1 1
    6  2 1  1 4 6  1  5 8  4 10 1 1 1 1 1 1 1 1 1 1
    > 
    > # find the column that should be left.
    > idx <- which(colMeans(d[(n+1):(2*n)], na.rm = TRUE) != 1)
    > 
    > # filter the data
    > d[, c(idx, idx+n)]
        A  B  C  D  F a b c d f
    1   5  5  2  7  3 0 1 1 1 1
    2   9  8  4  6  8 1 1 1 1 1
    3   6  6 10  3  6 1 1 1 1 1
    4   1  7  5  5 10 1 1 1 1 1
    5  10  9  6  2  5 1 1 0 1 1
    6   2  1  1  4  1 1 1 1 1 1
    7   8  4  7 10  2 1 1 1 1 0
    8   7  3  9  9  4 1 0 1 0 1
    9   3 10  3  1  9 1 1 0 1 1
    10  4  2  8  8  7 1 0 1 1 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know how to delete records for jqgrid, but i realise i cannot select
I know you can override delete and save methods in DJango models, but can
I know that you cannot delete a cookie set by another server in javascript.
Not sure how to work this but I found a way that works although
I'm not sure how to ask the question, for I don't know what I
I am getting error in updating columns using XML but don't know where is
This question was inspired by a similar question: How does delete[] know the size
The problem I'm running into is that as far as I know the delete
Does anybody know how to delete the complete svn repository including old revisions? I
Does anyone know how to delete an object and all of it's related entities.

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.