Rename .id column in ldply

plyr, r

Solution

You seem comfortable using setNames, so you can move the "j" and the "newname" assingment to a convenient wrapper.

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

Problem

Is there a way to assign a custom name to the column that is normally named `.id` in the 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` on the result, but I'd like to do it in one call. Any suggestions? Note that `adply` suffers from 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 related question addresses the issue of renaming the "data" columns, but the answer also fails to provide a solution for the `.id` column.

Original source

Related problems