Reputation: 267
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?
Upvotes: 0
Views: 73
Reputation: 214927
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)))]
Upvotes: 1