问题描述
我有一个看起来像这样的数据框
I have a dataframe which looks like this
var1<-c(150,20,30)
var2<-c(20,30,40)
x<-c(2.5,4.5,7.5)
s_var1<-c(0,0,0)
s_var2<-c(0,0,0)
data<-data.frame(var1, var2, x, s_var1, s_var2)
可能有一大堆var"列 - var1、var2....var'n'.与 s_var1、s_var2..... 等相同.
There could be a whole bunch of 'var' columns - var1, var2....var'n'. Same with s_var1, s_var2.....etc.
我想编写一个函数,在引用 'var' 列和 'x' 列的同时对 's_var' 列进行计算.
I want to write a function which does calculations on the 's_var' columns while referencing the 'var' columns and the 'x' column.
例如:如果有 2 个 'var' 列
For example: if there are 2 'var' columns
n_var<-c(1,2)
for (i in n_var)
{
if (x > 2.5) { s_var[i] = var[i] } else {s_var[i] = 2*var[i]}
}
有什么建议吗?我正在努力将列表中的数字作为后缀传递给引用列名...
Any suggestions? I am struggling to pass the numbers in the list as suffixes to reference column names...
推荐答案
使用grep
查找var
和s_var
的列索引列并定义依赖于 x
的 multiplier
.最后应用乘数.请注意,乘数将被循环使用以乘以 data[var_cols]
中的每一列.
Use grep
to find the column indexes of the var
and s_var
columns and define a multiplier
which depends on x
. Finally apply the multiplier. Note that the multiplier will be recycled to multiply each of the columns in data[var_cols]
.
fun <- function(data) {
var_cols <- grep("^var", names(data))
s_var_cols <- grep("^s_var", names(data))
multiplier <- (x > 2.5) + 1
data[s_var_cols] <- multiplier * data[var_cols] ##
data
}
fun(data)
给予:
var1 var2 x s_var1 s_var2
1 150 20 2.5 150 20
2 20 30 4.5 40 60
3 30 40 7.5 60 80
上面的## 行也可以这样写,这里不是真的需要,但如果计算更复杂,则可能需要使用 lapply
或 mapply
.
The ## line above could also be written like this, which is not really needed here but if the calculation were more complex then using lapply
or mapply
might be needed.
data[s_var_cols] <- lapply(data[var_cols], `*`, multiplier)
这篇关于对具有相似名称的变量执行相似计算的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!