I am trying to write a function to change the case of all of the text in a
data frame to lower case. 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"
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.
Specifying dat[1] as.character during the function call does not work (e.g.
tolower(as.character(dat[1]))).
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);
}
--
View this message in context:
http://r.789695.n4.nabble.com/Change-case-of-factor-in-data-frame-tp4651696.html
Sent from the R help mailing list archive at Nabble.com.
Ok, it seems that the function to get generic field names is get() res=names(dat); get(res[ind],pos=dat) will retrieve dat$name -- View this message in context: http://r.789695.n4.nabble.com/Change-case-of-factor-in-data-frame-tp4651696p4651919.html Sent from the R help mailing list archive at Nabble.com.
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