[R] Change case of factor in data frame
David Winsemius
dwinsemius at comcast.net
Mon Dec 3 22:15:53 CET 2012
On Dec 2, 2012, at 12:46 PM, Audrey wrote:
> I am trying to write a function to change the case of all of the
> text in a
> data frame to lower case.
Define what you mean by "text".
> I do not have foreknowledge of the data frame
> names or the data types of each column.
>
> It seems that if one references the data frame by index, then it
> returns
> class "data.frame" but if it is referenced by name, it returns class
> "factor" or whatever the column actually is:
> dat[1] returns class "data.frame" but
> dat$name returns class "factor"
Yes. That is correct.
>
> The problem is that, when one applies the tolower() and toupper()
> functions,
> dat$name will return a column of lower/uppercase returns (now class
> "character") but dat[1] will return an array of factor level indexes.
It return an integer vector with attributes: class = factor and levels.
> Specifying dat[1] as.character during the function call does not
> work (e.g.
> tolower(as.character(dat[1]))).
Because dat[1] is a list, not a vector.
>
> So, I would loop over the column names, but I can not figure out how
> to
> generically call them:
> tst=names(dat);
> dat$(tst[1]) returns an error
> dat[tst[1]] returns class data.frame again
>
> Thank you in advance!
>
> change_case<-function(dat,wcase){
> # change case
> res=sapply(dat,class); # get classes
> ind<-res=='character';
> dat[ind]<-switch(wcase,
> 'lower'=tolower(dat[ind]),
> 'upper'=toupper(dat[ind])
> )
> rm(ind);
> ind<-res=='factor';
> dat[ind]<-switch(wcase,
> 'lower'=factor(tolower(as.character(dat[ind]))),
> 'upper'=factor(toupper(as.character(dat[ind])))
> )
> return(dat);
> }
You probably need to study the help("[") page and learn the difference
between "[" and "[[". Changing to "[[" in a couple of places would
probably allow success.
--
David Winsemius, MD
Alameda, CA, USA
More information about the R-help
mailing list