[R] Writing a function to return column position XXXX

Mikko Korpela mikko.korpela at aalto.fi
Tue Jan 24 15:05:35 CET 2012


On 01/24/2012 03:49 PM, Dan Abner wrote:
> Hello everyone,
> 
> I am writing my own function to return the column index of all variables
> (these are currently character vectors) in a data frame that contain a
> dollar sign($). A small piece of the data look like this:
> 
>     can_sta can_zip ind_ite_con ind_uni_con AL 36106 $251,895.80 $22,874.43
> AL 35802 $141,373.60 $7,100.00  AL 35201 $273,208.50 $18,193.66  AR
> 72404 $186,918.00
> $25,391.00  AR 72217 $451,127.00 $27,255.23  AR 7.28E+08 $58,336.22 $5,293.82
> 
> 
> So far I have:
> 
> 
> col.id<-function(x) any(grep("$",x))
> sapply(cand2,col.id)
> 
> However, this returns TRUE for all columns (even those that do not contain
> the $).
> 
> Any assistance is appreciated.

One option is to use fixed = TRUE in the call to grep(). See ?grep.

BR,
-- 
Mikko Korpela
Aalto University School of Science
Department of Information and Computer Science



More information about the R-help mailing list