问题
I have a data frame that contains dates and id's. I need to add multiple columns to this data frame based on each date. I use ddply
to do this as follows:
ddply(df, "dt", transform, new_column1 = myfun(column_name_1))
However,I have a bunch of column names and would like to add multiple new columns. Is there a way that I can pass a string to transform instead of new_column1? For example I tried:
ddply(df, "dt", transform, get("some_column_name")=myfun(column_name_1))
but this does not work. Additionally, if I pass the column_name_1
to myfun as a string, can I just use get("column_name_1")
within myfun
to refer to the column?
UPDATE: NOT SURE HOW TO FORMAT THIS BETTER
input:
id date val
id1 d1 1
id2 d1 2
id3 d1 3
id4 d1 4
id1 d2 10
id2 d2 20
id3 d2 30
id4 d2 40
out (for 2 buckets for example)
id date val bucket
id1 d1 1 1
id2 d1 2 1
id3 d1 3 2
id4 d1 4 2
id1 d2 10 1
id2 d2 20 1
id3 d2 30 2
id4 d2 40 2
回答1:
Doing it with transform
is slick, but why not something more basic like
tmpf <- function(x) {
x[[new_column_name_1]] <- myfun(x[[column_name_1]])
x[[new_column_name_2]] <- myfun(x[[column_name_2]])
...
x
}
ddply(df,"dt",tmpf)
Or you can have a vector of column names to modify, or do it on the fly:
tmpf <- function(x,cols=c("column_name_1","column_name_2")) {
newcols <- paste("new",cols,sep="_")
for (i in seq_along(cols)) {
x[[newcols[i]]] <- myfun(x[[cols[i]]])
}
}
There's probably something even cleverer with assign
in the appropriate environment.
If I had a reproducible example I could test this.
来源:https://stackoverflow.com/questions/8391697/transform-data-frame-string-variable-names