transform data frame string variable names

て烟熏妆下的殇ゞ 提交于 2019-12-02 02:49:45

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!