[英]R - dynamically create columns using existing column names in sequence
I have a dataframe, df
, with several columns in it.我有一个 dataframe,
df
,其中有几列。 I would like to create a function to create new columns dynamically using existing column names.我想创建一个 function 以使用现有列名动态创建新列。 Part of it is using the last four characters of an existing column name.
其中一部分是使用现有列名的最后四个字符。 For example, I would like to create a variable names
df$rev_2002
like so:例如,我想创建一个变量名
df$rev_2002
,如下所示:
df$rev_2002 <- df$avg_2002 * df$quantity
The problem is I would like to be able to run the function every time a new column (say, df$avg_2003
) is appended to the dataframe.问题是我希望能够在每次将新列(例如
df$avg_2003
)附加到 dataframe 时运行 function。
To this end, I used the following function to extract the last 4 characters of the df$avg_2002
variable:为此,我使用了以下 function 来提取
df$avg_2002
变量的最后 4 个字符:
substRight <- function (x,n) {
substr(x, nchar(x)-n+1, nchar(x))
}
I tried putting together another function to create the columns:我尝试将另一个 function 放在一起来创建列:
revved <- function(x, y, z){
z = x * y
names(z) <- paste('revenue', substRight(x,4), sep = "_")
return x
}
But when I try it on actual data I don't get new columns in my df
.但是,当我对实际数据进行尝试时,我的
df
中没有新列。 The desired result is a series of variables in my df
such as:所需的结果是我的
df
中的一系列变量,例如:
df$rev_2002
, df$rev_2003
... df$rev_2020
or whatever is the largest value of the last four characters of the x
variable ( df$avg_2002
in example above). df$rev_2002
, df$rev_2003
... df$rev_2020
或x
变量最后四个字符的最大值(上面示例中的df$avg_2002
)。
Any help or advice would be truly appreciated.任何帮助或建议将不胜感激。 I'm really in the woods here.
我真的在这里的树林里。
dat <- data.frame(id = 1:2, quantity = 3:4, avg_2002 = 5:6, avg_2003 = 7:8, avg_2020 = 9:10)
func <- function(dat, overwrite = FALSE) {
nms <- grep("avg_[0-9]+$", names(dat), value = TRUE)
revnms <- gsub("avg_", "rev_", nms)
if (!overwrite) revnms <- setdiff(revnms, names(dat))
dat[,revnms] <- lapply(dat[,nms], `*`, dat$quantity)
dat
}
func(dat)
# id quantity avg_2002 avg_2003 avg_2020 rev_2002 rev_2003 rev_2020
# 1 1 3 5 7 9 15 21 27
# 2 2 4 6 8 10 24 32 40
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.