Rename .id column to ldply

Is there a way to assign a custom name to a column that is usually called .id as a result of ldply ?

 > ldply(setNames(1:3, 1:3), function(i) data.frame(j=1/i)) .id j 1 1 1.0000000 2 2 0.5000000 3 3 0.3333333 

I know I can call rename as a result, but I would like to do it in one call. Any suggestions?

Note that adply experiencing a similar problem:

 > adply(as.array(setNames(1:3, 1:3)), 1, function(i) data.frame(j=1/i)) X1 j 1 1 1.0000000 2 2 0.5000000 3 3 0.3333333 

Another question relates to the problem of renaming data columns, but the answer also does not provide a solution for the .id column.

+4
source share
2 answers

It’s more convenient for you to use setNames, so you can move β€œj” and β€œnew name” to a convenient shell.

 setNames( ldply(setNames(1:3, 1:3), function(i) data.frame(1/i)) , c("newname", "j") ) newname j 1 1 1.0000000 2 2 0.5000000 3 3 0.3333333 
+3
source

I have a suggested implementation , let's see if it turns into an official plyr . In principle, the new parameter .idname , which allows you to specify the name of the .id column, with the option to refuse it altogether, passing NULL :

 > ldply(setNames(1:3, 1:3), function(i) data.frame(j=1/i), .idname='i') ij 1 1 1.0000000 2 2 0.5000000 3 3 0.3333333 

. Install the modified version using

 library(devtools) install_github('plyr', 'krlmlr', ref='140-142-id') 

EDIT : this is now available in plyr 1.8.1 on CRAN:

 > ldply(setNames(nm=1:3), function(i) data.frame(j=1/i), .id='i') 
+4
source

All Articles