(By the way, I use = for assignment, not <-, in the below.) Here is the long-winded approach; setting a column to NULL removes it:
z$close1=NULL z$close2=NULL z$close3=NULL z$close4=NULL z$close5=NULL z$open1=NULL z$open2=NULL z$open3=NULL z$open4=NULL z$open5=NULLHowever using a string for the column name won't work. In other words, these don't work:
z['open1']=NULL #This one does work for data frames though. z[['open1']]=NULLThe solution I eventually worked out is:
z=z[, setdiff(colnames(z),c('close1','close2','close3','close4','close5','open1','open2','open3','open4','open5')) ]colnames(z) gets the current list of columns.
I then use setdiff() to subtract from that the list of columns I want to remove.
The z[,...] syntax means take a copy with just these columns (keeping all rows).
If you don't understand my motivation, I should first say I had more like 30 fields, not just the 10 shown in the example above. But also I like doing things programmatically, as it can avoid introducing typos. For instance, the above solution could be rewritten as:
#Remove the openN and closeN columns z=z[, setdiff(colnames(z),c(paste('close',1:5,sep=''),paste('open',1:5,sep='') )) ]