如果我使用字符向量引用列,gsub() 不起作用?

gsub() not working if I reference a column using a character vector?

注意:我是 R 的新手,感谢所有帮助。我们都从某个地方开始!

我想对旨在提高我的 R 技能的项目执行以下操作:

preferences[, (cols) := gsub(cols, pattern = "UN1", replacement = "A")]

cols 是一个只包含一个条目的向量:名为“Pref_1”的列的名称。我的 objective 是让 R 将“Pref_1”列中所有出现的“UN1”替换为字母“A”。 运行 上面的代码不起作用,而是在每个条目中粘贴“Pref_1”:

    Pref_1
 1: Pref_1
 2: Pref_1
 3: Pref_1
 4: Pref_1
 5: Pref_1
 6: Pref_1
 7: Pref_1
 8: Pref_1
 9: Pref_1
10: Pref_1

通过实验,我发现如果我 运行 一个其他方面相同的代码,而是用 Pref_1 替换 cols(其中包含字符“Pref_1”),代码按预期执行:

preferences[, (cols) := gsub(Pref_1, pattern = "UN1", replacement = "A")]
                                                                               Pref_1
 1:                                                                                 A
 2:                                           Food and Agriculture Organization (FAO)
 3:         United Nations Educational, Scientific and Cultural Organization (UNESCO)
 4:                                       United Nations Development Programme (UNDP)
 5:                                                Commission on Narcotic Drugs (CND)
 6:                                                Commission on Narcotic Drugs (CND)
 7:                                                        Human Rights Council (HRC)
 8:                                                                                 A
 9:                                                        Human Rights Council (HRC)
10:                                                                                 A

为什么 gsub() 不允许我使用 cols 对象引用我想操作的列?我之所以坚持使用cols对象来引用我要操作的列,是因为我想给cols加上列名,然后运行一个for循环遍历指定的列。如果不使用 cols 向量循环,我将不得不复制此代码 n 次以对 n 列进行操作。

gsub 被赋予一个字符串向量,它会做它知道的事情:处理字符串。它不知道它们应该是间接引用。 (没什么会知道应该是间接的。)

您有两个选择:

  1. data.table 中的规范方法很可能使用 .SDcols

    preferences[, (cols) := lapply(.SD, gsub, pattern = "UN1", replacement = "A"), .SDcols = cols]
    preferences
    #                                      Pref_1
    #                                      <char>
    #  1:                                       A
    #  2: Food and Agriculture Organization (F...
    #  3: United Nations Educational, Scientif...
    #  4: United Nations Development Programme...
    #  5:      Commission on Narcotic Drugs (CND)
    #  6:      Commission on Narcotic Drugs (CND)
    #  7:              Human Rights Council (HRC)
    #  8:                                       A
    #  9:              Human Rights Council (HRC)
    # 10:                                       A
    

    这有两件事:(i) 使用 .SDcols 迭代一组动态列是首选且速度更快,并且允许以编程方式确定这些列(您需要什么); (ii) 使用 lapply 允许您对 一个或多个 列执行此操作。如果您知道您将始终只做一列,那么这仍然可以很好地工作,而且开销很小。

  2. 您可以get/mget数据。这是告诉某些东西获取字符串向量中标识的变量的内容的方法。

    如果您知道您总是只有一列,那么您可以使用 get:

    preferences[, (cols) := gsub(get(cols), pattern = "UN1", replacement = "A")]
    

    即使有 机会 你会有不止一个,我强烈推荐 mget。 (即使你认为你会一直拥有一个,这仍然是安全的。)

    preferences[, (cols) := lapply(mget(cols), gsub, pattern = "UN1", replacement = "A")]
    

数据

preferences <- setDT(structure(list(Pref_1 = c("UN1", "Food and Agriculture Organization (FAO)", "United Nations Educational, Scientific and Cultural Organization (UNESCO)", "United Nations Development Programme (UNDP)", "Commission on Narcotic Drugs (CND)", "Commission on Narcotic Drugs (CND)", "Human Rights Council (HRC)", "UN1", "Human Rights Council (HRC)", "UN1")), class = c("data.table", "data.frame"), row.names = c(NA, -10L)))
cols <- "Pref_1"