【问题标题】:Error setting column name R错误设置列名 R
【发布时间】:2018-01-24 06:35:55
【问题描述】:

我正在尝试重命名 R 中的一列。我有一个列表并想重命名该列表中数据框的一列。

这是我的控制台的摘录。

> colnames(Prem[["TasNSW"]])
 [1] "Master Client Number" "Master Client Name"   "Policy Number"        "State"               
 [5] "Policy Name"          "Year"                 "Effective Date"       "Expiry Date"         
 [9] "Estimate/Actual"      "Total Wages"          "Master Client Name  "
> colnames(Prem[["TasNSW"]][10]) <- "Remuneration"
> colnames(Prem[["TasNSW"]])
 [1] "Master Client Number" "Master Client Name"   "Policy Number"        "State"               
 [5] "Policy Name"          "Year"                 "Effective Date"       "Expiry Date"         
 [9] "Estimate/Actual"      "Total Wages"          "Master Client Name  "

我希望看到的是

> colnames(Prem[["NSW"]])
 [1] "Master Client Number" "Master Client Name"   "Policy Number"        "State"               
 [5] "Policy Name"          "Year"                 "Effective Date"       "Expiry Date"         
 [9] "Estimate/Actual"      "Total Wages"          "Master Client Name  "
> colnames(Prem[["NSW"]][10]) <- "Remuneration"
> colnames(Prem[["NSW"]])
 [1] "Master Client Number" "Master Client Name"   "Policy Number"        "State"               
 [5] "Policy Name"          "Year"                 "Effective Date"       "Expiry Date"         
 [9] "Estimate/Actual"      "Remuneration"         "Master Client Name  "

我已经多次使用该代码,所以我认为问题是基于它在列表中的存在。我错过了什么?

【问题讨论】:

  • 你需要移动) - colnames(Prem[["TasNSW"]])[10] &lt;- "Remuneration"

标签: r list dataframe


【解决方案1】:

你把右括号放错了:

colnames(Prem[["TasNSW"]][10]) <- "Remuneration"

试试这个:

colnames(Prem[["TasNSW"]])[10] <- "Remuneration"

你会得到预期的结果

【讨论】:

    猜你喜欢
    • 2013-10-21
    • 2014-11-04
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多