0

I have a dataframe df that I want to subset in the following way:

Columns 1-7 and a vector of columns cols:

cols = c("rs1057079", "rs1057079.1", "rs4845882", "rs4845882.1", "rs1891932", "rs1891932.1", "rs530296", "rs530296.1", "rs10497340", "rs10497340.1")

So what I did is df[, c(1:7, cols)] but R throws an error:

Error in `[.data.frame`(df, , c(seq(1:7), SNPs_dup)) : 
  undefined columns selected

What is wrong here?? I can subset on 1:7 and cols but why not on both of them?

1 Answer 1

1

c(1:7, cols) coerces the sequence 1:7 to characters which are further treated as columns names '1', '2' ... instead of column positions, you can extract the 1-7 column names and then concatenate with cols and subset:

df[,c(names(df)[1:7], cols)]

Or convert cols to positions:

df[,c(1:7, match(cols, names(df)))]
Sign up to request clarification or add additional context in comments.

2 Comments

I see, but how to avoid c() to do this? This is the only way?
You can't stop c from doing this, since it produces an atomic vector which must have the same data type. I provided an alternative way for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.