Daniel V
Daniel V

Reputation: 1386

Error setting column name R

I'm trying to rename a column in R. I have a list and want to rename one column of a dataframe within that list.

Here's an excerpt from my Console.

> 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  "

What I'd expect to see is

> 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  "

I've used that code a number of times so I think the issue is based on its presence in a list. What have I missed?

Upvotes: 0

Views: 51

Answers (1)

HAVB
HAVB

Reputation: 1888

You misplaced the closing parentheses here:

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

Instead, try this:

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

And you'll get the expected result

Upvotes: 3

Related Questions