如何分配字符串而不是动态数字作为给定特定数据帧的值?

How to assign strings instead of on-the-fly numbers as the value of the given specific dataframe?

(给出了可重现的例子)下面的函数causfinder::causalitycombinations

causalitycombinations <- function (nvars, ncausers, ndependents) 
{
    independents <- combn(nvars, ncausers)
    swingnumber <- dim(combn(nvars - ncausers, ndependents))[[2]]
    numberofallcombinations <- dim(combn(nvars, ncausers))[[2]] * swingnumber
    dependents <- matrix(, nrow = dim(combn(nvars, ncausers))[[2]] * swingnumber, ncol = ndependents)
    for (i in as.integer(1:dim(combn(nvars, ncausers))[[2]])) {
        dependents[(swingnumber * (i - 1) + 1):(swingnumber * i), ] <- t(combn(setdiff(seq(1:nvars), independents[, i]), ndependents))
    }
    swingedindependents <- matrix(, nrow = dim(combn(nvars, ncausers))[[2]] * swingnumber, ncol = ncausers)
    for (i in as.integer(1:dim(combn(nvars, ncausers))[[2]])) {
        for (j in as.integer(1:swingnumber)) {
            swingedindependents[(i - 1) * swingnumber + j, ] <- independents[, i]
        }
    }
    independentsdependents <- cbind(swingedindependents, dependents)
    others <- matrix(, nrow = dim(combn(nvars, ncausers))[[2]] * swingnumber, ncol = nvars - ncausers - ndependents)
    for (i in as.integer(1:((dim(combn(nvars, ncausers))[[2]]) * 
        swingnumber))) {
        others[i, ] <- setdiff(seq(1:nvars), independentsdependents[i, ])
    }
    causalitiestemplate <- cbind(independentsdependents, others)
    causalitiestemplate
}

列出所有多元因果组合。例如,在一个 4 变量系统中,以系统的其他 2 个变量为条件,它们是(当变量分配给数字 1、2、3、4 并且该分配在整个分析过程中保持时):

causalitycombinations(4,1,1)

      [,1] [,2] [,3] [,4]
 [1,]    1    2    3    4
 [2,]    1    3    2    4
 [3,]    1    4    2    3
 [4,]    2    1    3    4
 [5,]    2    3    1    4
 [6,]    2    4    1    3 # to check whether 2nd var Grangercauses 4th var condioned on 1 and 3
 [7,]    3    1    2    4
 [8,]    3    2    1    4
 [9,]    3    4    1    2
[10,]    4    1    2    3
[11,]    4    2    1    3
[12,]    4    3    1    2

现在,

data.frame(from = causalitycombinations(4,1,1)[,1], to= causalitycombinations(4,1,1)[,2],
                 pval = c(0.5,0.6,0.1, #I just typed random p-vals here
                          0.4,0.8,0.2,
                          0.1,0.5,0.9,
                          0.0,0.0,0.1)
                 )

产生:

   from to  pval
1     1  2 0.5
2     1  3 0.6
3     1  4 0.1
4     2  1 0.4
5     2  3 0.8
6     2  4 0.2
7     3  1 0.1
8     3  2 0.5
9     3  4 0.9
10    4  1 0.0
11    4  2 0.0
12    4  3 0.1
  1. 在上面的 "from" 和 "to" 列的条目中,我想打印变量的名称(例如:"inf"、"gdp"、"exc", "stock") 而不是他们的代表数字(即 1,2,3,4)。如何做到这一点?

  2. 等价地,如何用字符串而不是数字列出组合

我们可以根据字符串向量中的位置更新具有匹配名称的列:

# update columns with matching name
df1$from <- c("inf", "gdp", "exc", "stock")[df1$from]
df1$to <- c("inf", "gdp", "exc", "stock")[df1$to]

# result
df1
#     from    to pval
# 1    inf   gdp  0.5
# 2    inf   exc  0.6
# 3    inf stock  0.1
# 4    gdp   inf  0.4
# 5    gdp   exc  0.8
# 6    gdp stock  0.2
# 7    exc   inf  0.1
# 8    exc   gdp  0.5
# 9    exc stock  0.9
# 10 stock   inf  0.0
# 11 stock   gdp  0.0
# 12 stock   exc  0.1

# input data
df1 <- read.table(text="   from to  pval
1     1  2 0.5
2     1  3 0.6
3     1  4 0.1
4     2  1 0.4
5     2  3 0.8
6     2  4 0.2
7     3  1 0.1
8     3  2 0.5
9     3  4 0.9
10    4  1 0.0
11    4  2 0.0
12    4  3 0.1", header = TRUE)