使用rxSetVarInfo更改动态变量名称 [英] Change a dynamic variable name with rxSetVarInfo
问题描述
尝试使用rxSetVarInfo更改XDF的变量名称.
Trying to change a variable name of an XDF with rxSetVarInfo.
我想用通用的var名称合并几个数据集. (我知道rxMerge可以/将在需要时追加到文件名.我想拥有更多的控制权.)
I want to merge several data sets with common var names. (I know rxMerge can/will append to filenames where needed. I want to have more control than that.)
这有效:
outLetter<- "A"
exp <- list(pct.A = list(newName = paste0("X.pct.",outLetter)))
rxSetVarInfo(varInfo = exp, data = tempXDFFile)
那是我知道原始列名pct.A
的地方.如果那是动态的怎么办?如果这是在使用不同的outLetter
多次调用的函数中该怎么办. ("A"不是硬编码的.)
这不起作用:
That's where I know the original column name, pct.A
. What if that's dynamic? What if this is in a function that gets called several times with different outLetter
's. (The "A" isn't hardcoded.)
This does not work:
function(outLetter){
exp <- list(paste0("pct.",outLetter) = list(newName = paste0("X.pct.",outLetter)))
rxSetVarInfo(varInfo = exp, data = tempXDFFile)
}
也没有:
exp <- parse(text = exp)
rxSetVarInfo(varInfo = exp, data = tempXDFFile)
是的,我可以对所有排列进行硬编码.试图找到一种更优雅的方法.
Yes, I can hardcode all the permutations. Trying to find a more elegant approach.
推荐答案
请尝试以下代码:
dynamicName <- function(outLetter){
exp <- vector(mode="list", length=1)
names(exp) <- paste0("pct.",outLetter)
exp[[paste0("pct.",outLetter)]] = list(newName = paste0("X.pct.",outLetter))
rxSetVarInfo(varInfo = exp, data = tempXDFFile)
}
在调用rxSetVarInfo()之前,"exp"包含:
Before the call to rxSetVarInfo(), "exp" contains:
$pct.A
$pct.A$newName
[1] "X.pct.A"
运行您的可行"案例,我看到:
Running your "this works" case, I see:
> outLetter<- "A"
> exp <- list(pct.A = list(newName = paste0("X.pct.",outLetter)))
>
> exp
$pct.A
$pct.A$newName
[1] "X.pct.A"
希望这会有所帮助!
请注意,请确保您的动态命名功能可以访问变量"tempXDFFile",您可能需要考虑将其作为参数传递,例如:
Note, please make sure that your dynamic naming function has access to the variable "tempXDFFile", you may want to consider passing it as a parameter, like:
dynamicName <- function(outLetter, data){
exp <- vector(mode="list", length=1)
names(exp) <- paste0("pct.",outLetter)
exp[[paste0("pct.",outLetter)]] = list(newName = paste0("X.pct.",outLetter))
rxSetVarInfo(varInfo = exp, data = data)
}
这篇关于使用rxSetVarInfo更改动态变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!