【问题标题】:Insert Column Name into its Value using R使用 R 将列名插入其值
【发布时间】:2018-03-08 12:32:48
【问题描述】:

我需要在其值中插入列名、部门。我有这样的代码:

Department <- c("Store1","Store2","Store3","Store4","Store5")
Department2 <- c("IT1","IT2","IT3","IT4","IT5")
x <- c(100,200,300,400,500)
Result <- data.frame(Department,Department2,x)
Result

预期的结果是这样的:

Department <- c("Department_Store1","Departmentz_Store2","Department_Store3","Department_Store4","Department_Store5")
Department2 <- c("Department2_IT1","Department2_IT2","Department2_IT3","Department2_IT4","Department2_IT5")
x <- c(100,200,300,400,500)
Expected.Result <- data.frame(Department,Department2,x)
Expected.Result

有人可以帮忙吗?谢谢

【问题讨论】:

  • 使用paste,即Result$Department &lt;- paste(names(Result)[1], Result$Department, sep="_")

标签: r text dplyr data.table reshape2


【解决方案1】:

dplyrtidyr 的另一种方式:

library(dplyr)
library(tidyr)

# Convert to character to avoid warning message, will convert all columns to character
Result[] <- lapply(Result, as.character)

Result %>%
  mutate_if(is.factor, as.character) %>% # optional, only convert factor to character, retain all other types
  gather(key, value, -x) %>% 
  mutate(var = paste(key, value, sep = "_")) %>% 
  select(-value) %>% 
  spread(key,var)

    x        Department     Department2
1 100 Department_Store1 Department2_IT1
2 200 Department_Store2 Department2_IT2
3 300 Department_Store3 Department2_IT3
4 400 Department_Store4 Department2_IT4
5 500 Department_Store5 Department2_IT5

数据:

Result <- data.frame(
  Department = c("Store1","Store2","Store3","Store4","Store5"),
  Department2 = c("IT1","IT2","IT3","IT4","IT5"),
  x = c(100,200,300,400,500)
)

【讨论】:

  • 谢谢。它对我有帮助,但如果我有 10 个具有不同价值的部门。有商店的部门(如上)、有 IT 的部门、有财务的部门等。我应该怎么做?我当然不能一一做。谢谢之前
  • 我不确定我是否遵循,您能否使用显示上述代码未能产生所需输出的数据更新问题?例如,如果Department 中的第 1 行的值为 IT,则上面的代码应为第 1 行返回 Department_IT。这是您想要的吗?
  • 您好,先生,我已经更新了我的数据。你能帮我解决这个问题吗?谢谢
  • 更新了我的答案,如果这就是你想要的,请告诉我
  • 谢谢先生..了不起的工作,非常感谢:)
【解决方案2】:

如果您将相关列名收集到向量 dep_col 中,这是一个带有 for 循环的干净 base R 解决方案:

df <- data.frame(x = 1:5,
                 Department = paste0("Store", 1:5),
                 Department2 = paste0("IT", 1:5))

dep_col <- names(df)[-1]

for (c in dep_col)
  df[[c]] <- paste(c, df[[c]], sep = "_")

【讨论】:

    【解决方案3】:

    如果我理解正确,OP 希望在所有以 "Department" 开头的列中添加相应的列名。

    编辑应 OP 的要求,选择列的代码已被概括为选择其他列名。

    这是使用data.table的快速set()函数的解决方案:

    library(data.table)
    setDT(Result)
    cols <- stringr::str_subset(names(Result), "^(Department|Division|Team)")
    for (j in cols) {
      set(Result, NULL, j, paste(j, Result[[j]], sep = "_"))
    }
    Result
    
              Department     Department2   x
    1: Department_Store1 Department2_IT1 100
    2: Department_Store2 Department2_IT2 200
    3: Department_Store3 Department2_IT3 300
    4: Department_Store4 Department2_IT4 400
    5: Department_Store5 Department2_IT5 500
    

    请注意,set() 通过引用更新,即不复制整个对象。

    【讨论】:

    • 嗨,实际上我的数据并不总是以部门先生开头,它可以以部门、团队等开头。部门是一个样本。先生能不能说的更笼统一些?谢谢
    • 代码已泛化。您可以使用 | 运算符自行决定添加更多内容。
    猜你喜欢
    • 2020-01-20
    • 2013-07-11
    • 2016-06-17
    • 2018-08-20
    • 1970-01-01
    • 2019-04-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多