0

How to change Row name of a particular column in R dataframe:

<df

Name    Value
A       10
B       20
C       30

I want output to be:

Name    Value
AAA     10
BBB     20
CCC     30

I have tried data.table library but it couldn't work.

library(data.table)
my_df <- setattr(df$Name, "row.names", c("AAA", "BBB", "CCC"))
1
  • Tried this but it's not changing the Values under "Name" head, it created new row name. ~ Name Value AAA A 10 BBB B 20 CCC C 30~ Commented Jan 18, 2018 at 18:11

1 Answer 1

3

Looks like the name column is it's own column. See the code below

sample <- data.frame(Name = c("A", "B", "C"), Value = c(10,20,30))

Looks like this:

  > sample
 Name Value
1    A    10
2    B    20
3    C    30

Now Try:

sample$Name <- c("AAA", "BBB", "CCC")

Yields:

> sample
  Name Value
1  AAA    10
2  BBB    20
3  CCC    30
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.