使用 rxSetVarInfo 更改动态变量名称
Change a dynamic variable name with rxSetVarInfo
正在尝试使用 rxSetVarInfo 更改 XDF 的变量名称。
我想合并几个具有通用变量名的数据集。 (我知道 rxMerge can/will 在需要的地方附加到文件名。我想拥有比这更多的控制权。)
这个有效:
outLetter<- "A"
exp <- list(pct.A = list(newName = paste0("X.pct.",outLetter)))
rxSetVarInfo(varInfo = exp, data = tempXDFFile)
这就是我知道原始列名称 pct.A
的地方。如果那是动态的呢?如果这是在使用不同 outLetter
多次调用的函数中怎么办。 ("A" 不是硬编码的。)
这不起作用:
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)
是的,我可以对所有排列进行硬编码。试图找到更优雅的方法。
请试试这个代码:
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" 包含:
$pct.A
$pct.A$newName
[1] "X.pct.A"
运行 你的 "this works" 案例,我明白了:
> outLetter<- "A"
> exp <- list(pct.A = list(newName = paste0("X.pct.",outLetter)))
>
> exp
$pct.A
$pct.A$newName
[1] "X.pct.A"
希望对您有所帮助!
注意,请确保您的动态命名函数可以访问变量 "tempXDFFile",您可能需要考虑将其作为参数传递,例如:
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 更改 XDF 的变量名称。
我想合并几个具有通用变量名的数据集。 (我知道 rxMerge can/will 在需要的地方附加到文件名。我想拥有比这更多的控制权。)
这个有效:
outLetter<- "A"
exp <- list(pct.A = list(newName = paste0("X.pct.",outLetter)))
rxSetVarInfo(varInfo = exp, data = tempXDFFile)
这就是我知道原始列名称 pct.A
的地方。如果那是动态的呢?如果这是在使用不同 outLetter
多次调用的函数中怎么办。 ("A" 不是硬编码的。)
这不起作用:
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)
是的,我可以对所有排列进行硬编码。试图找到更优雅的方法。
请试试这个代码:
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" 包含:
$pct.A
$pct.A$newName
[1] "X.pct.A"
运行 你的 "this works" 案例,我明白了:
> outLetter<- "A"
> exp <- list(pct.A = list(newName = paste0("X.pct.",outLetter)))
>
> exp
$pct.A
$pct.A$newName
[1] "X.pct.A"
希望对您有所帮助!
注意,请确保您的动态命名函数可以访问变量 "tempXDFFile",您可能需要考虑将其作为参数传递,例如:
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)
}