[R] Change case of factor in data frame
Audrey
barnett.audrey at gmail.com
Sun Dec 2 21:46:06 CET 2012
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.
More information about the R-help
mailing list