1

I would like to remove columns that have less than 150 from the column sum from my dataframe df1

My dataframe is

chr      leftPos    FLD0195  FLD0197 FLD0201 FLD0203 FLD0211    FLD0243
chr1    100260254       34    52       29        18    13       30
chr1    100735342       44   111       88        65    40       66
chr1    100805662        0    0         1         1    0         0
chr1    100839460        1    0         5         0    0         0

The formula I'm using is as below. It runs without error but df2 is exactly the same as df1

    df2 <- df1[,(colSums(df1[,3:ncol(df1)]) > 100000),]
5
  • Do you mean remove rows that have less than 150 from the column sum? Commented Mar 23, 2015 at 17:26
  • 2
    Your problem is that you are checking sums only for df1[,3:ncol(df1)] while sub-setting from the whole data set. You should do something like cbind(df[1:2], df1[3:ncol(df1)][, colSums(df1[3:ncol(df1)]) > 150, drop = FALSE]) Commented Mar 23, 2015 at 17:31
  • OK so how to subset based on colSums >10000 Commented Mar 23, 2015 at 17:34
  • Change 150 to 10000 in @DavidArenburg's code. Commented Mar 23, 2015 at 17:35
  • A better option would be just df1[c(TRUE, TRUE, colSums(df1[3:ncol(df1)]) > 150)] (adjust 150 to 1e5 in your original data) Commented Mar 23, 2015 at 17:36

1 Answer 1

4

When running a Boolean expression on a k columns subset you are receiving a k size logical vector. When entered inside a n size column data set, n-k values from the beginning of the vector are being recycled (until it reaches the size of n), thus wrong columns are being selected. In your case, the fix is simple, just add n-k TRUE values at the beginning of the logical vector (because you want to keep all the n-k columns at the beginning)

df1[c(rep(TRUE, 2L), colSums(df1[3L:ncol(df1)]) > 150L)]
#    chr   leftPos FLD0197
# 1 chr1 100260254      52
# 2 chr1 100735342     111
# 3 chr1 100805662       0
# 4 chr1 100839460       0
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.