[R] Change case of factor in data frame

David Winsemius dwinsemius at comcast.net
Mon Dec 3 22:15:43 CET 2012


On Dec 3, 2012, at 9:47 AM, Audrey wrote:

> Ok, it seems that the function to get generic field names is get()
>

Er, not really.

> res=names(dat);
> get(res[ind],pos=dat) will retrieve dat$name


There are far less baroque was of doing that (including dat$name and  
dat[["name"]]. Read:

?"["

If you had a numeric object, 'ind' then

dat[ind] would retrieve a sub-list, with as many columns as there were  
items in 'ind' and would have class data.frame.

If `ind` is numeric with one element only, then

dat[[ind]] <- tolower( as.character (dat[[ind]]  ) )  # should succeed  
at least to the extent of returning a vector of a different class ...  
if what were what you wanted as to change .

It would have been faster to change just the levels attributes for  
factor columns if you were willing to retain the class of that column  
as a factor.

levels(dat[[1]])<- toupper( levels(dat[[1]] )


-- 
David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list