[R] rename and concatenate name of columns

David Winsemius dwinsemius at comcast.net
Sat Jun 15 02:45:46 CEST 2013


On Jun 14, 2013, at 1:25 PM, Bert Gunter wrote:

> For the record:
> 
> ...
> 
> ------------
>> 
>> A bit of commentary: Something did happen. It's just that you didn't do anything with _what_ happened. The copy of the 'dataset'-object got modified but you never returned it from the function, and and also didn't reassign it to the original 'dataset'. Functions return their last assignment.

This line was probably more worthy of criticism. Functions return the result of last evaluated expression. (Which is often not what the beginning R programmer expected.)

>> In the case of the 'for'-function, it somewhat surprisingly returns a NULL. It is a rather odd function in the functional R world, since its main role in life is doing things by side-effects,
> ---------
> 
> for() is **not** a function.
> 
> ?"for" describes it as a "basic control-flow [sic] construct."

Nonetheless it does return NULL.
> 
> Ergo, it's behavior is not odd.

That remains a matter of opinion.

-- 
David


> 
> 
> Cheers,
> Bert
> 
> 
> 
> and so when used inside functions has seemingly paradoxical behavior.
> Check your results with:
>> 
>> rename_columns <- function(dataset) {
>> for (i in 2:(ncol(dataset))) {names(dataset)[i] <- paste(names(dataset)[1],
>> names(dataset)[i], sep="_")
>> }
>> dataset}
>> 
>> dataset <- rename_columns(dataset)
>> 
>>>      [[alternative HTML version deleted]]
>> 
>> And do learn to post in plain text.
>>> 
>>> ______________________________________________
>>> R-help at 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.
>> 
>> David Winsemius
>> Alameda, CA, USA
>> 
>> ______________________________________________
>> R-help at 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.
> 
> 
> 
> -- 
> 
> Bert Gunter
> Genentech Nonclinical Biostatistics
> 
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list