[R] rename and concatenate name of columns

Bert Gunter gunter.berton at gene.com
Fri Jun 14 22:25:26 CEST 2013


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

Ergo, it's behavior is not odd.


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



More information about the R-help mailing list