Re: [R] Change case of factor in data frame

2012-12-03 Thread Audrey
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.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Change case of factor in data frame

2012-12-03 Thread David Winsemius


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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Change case of factor in data frame

2012-12-03 Thread David Winsemius


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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Change case of factor in data frame

2012-12-03 Thread Audrey
 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]].

Both dat$name and dat[[name]] require you to know what name is. I was
looking for a way to retrieve a data frame column by name without actually
knowing what the data frame column name was. get() seems to do that, but I
am open to other options. 

Thank you for your advice regarding [ vs. [[. Indeed, lower(dat[[ind]]) does
return the desired result. However, it seems that ind can only be a single
integer (or evaluate to TRUE for only 1 column), I guess because [[ is
returning a vector. 

By text I mean anything that was non-numerical: character and factor
classes, in my case. 



--
View this message in context: 
http://r.789695.n4.nabble.com/Change-case-of-factor-in-data-frame-tp4651696p4651971.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Change case of factor in data frame

2012-12-03 Thread David Winsemius


On Dec 3, 2012, at 3:34 PM, Audrey wrote:


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]].

Both dat$name and dat[[name]] require you to know what name is.  
I was
looking for a way to retrieve a data frame column by name without  
actually
knowing what the data frame column name was. get() seems to do that,  
but I

am open to other options.


And I gave you some, but you failed to include context, an annoying  
habit of Nabble users. (In my opinion the Nabble interface is more  
hassle than it is worth.)  For instance, I wrote:




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.








Thank you for your advice regarding [ vs. [[. Indeed,  
lower(dat[[ind]]) does
return the desired result. However, it seems that ind can only be a  
single

integer


That was what I wrote I believe.


(or evaluate to TRUE for only 1 column),


In some instances TRUE is coerced to 1.


I guess because [[ is returning a vector.


No. You have failed to understand that [[ can only return a single  
referenced object. It might be a vector or a list, but you cannot give  
it a vector with more than one element and expect satisfactory results.




By text I mean anything that was non-numerical: character and factor
classes, in my case.

Then you should be clear. text is not a well defined term when  
referring to R objects.



View this message in context: 
http://r.789695.n4.nabble.com/Change-case-of-factor-in-data-frame-tp4651696p4651971.html
Sent from the R help mailing list archive at Nabble.com.


No. This borders on actionable fraud.  R-help is not Nabble. Do not  
believe what Nabble is saying. Do read the Rhelp Posting Guide.




--

David Winsemius, MD
Alameda, CA, USA

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.