How to subset a Data frame column wise using column names?

I have created a data frame named z.

  a = c(1,1,1);
  b = c(2,2,2);
  c = c(3,3,3);
  d = c(4,4,4);
  z = data.frame(a,b,c,d);

I want to remove column c and d from data frame z.

I tried this code

p = subset(z , colnames(z) == c('a' , 'b'))

But i am getting this result

a   b   c   d
1   2   3   4   
1   2   3   4 

What changes should i make in this command to remove column c and d from z.

Leave a Comment