RodLL
RodLL

Reputation: 110

Renaming columns in a data frame

I have the following data frame:

structure(list(D1 = structure(list(t.t.scale.x.....sd...mean. = c(95.0871563564004, 88.072473285318, 116.840370358282)), class = "data.frame", row.names = c(NA, -3L)), D2 = structure(list(t.t.scale.x.....sd...mean. = c(96.7679763688573, 116.352548635184, 86.8794749959586)), class = "data.frame", row.names = c(NA, -3L)), D3 = structure(list(t.t.scale.x.....sd...mean. = c(92.420897699915, 90.3018565270302, 117.277245773055)), class = "data.frame", row.names = c(NA, -3L)), D4 = structure(list(t.t.scale.x.....sd...mean. = c(96.6694428192656, 86.945205844304, 116.38535133643)), class = "data.frame", row.names = c(NA, -3L)), D5 = structure(list(t.t.scale.x.....sd...mean. = c(82.8720020998492, 106.333804799486, 110.794193100665)), class = "data.frame", row.names = c(NA, -3L))), class = "data.frame", row.names = c("GG", "GV", "AR"))

I am trying to rename the columns using:

colnames(x) <- c("a", "b","c","d","e") 

But the names do not change at all.

Upvotes: 0

Views: 58

Answers (1)

Andre Wildberg
Andre Wildberg

Reputation: 19191

I can replicate your finding. Its probably because your structure has two or more structures stacked.

dat
   t.t.scale.x.....sd...mean. t.t.scale.x.....sd...mean.
GG                   95.08716                   96.76798
GV                   88.07247                  116.35255
AR                  116.84037                   86.87947
   t.t.scale.x.....sd...mean. t.t.scale.x.....sd...mean.
GG                   92.42090                   96.66944
GV                   90.30186                   86.94521
AR                  117.27725                  116.38535
   t.t.scale.x.....sd...mean.
GG                    82.8720
GV                   106.3338
AR                   110.7942

colnames(dat)
[1] "D1" "D2" "D3" "D4" "D5"

You can resolve this by casting to matrix and then back to data frame

dat <- data.frame( as.matrix( dat ) )
dat
          D1        D2        D3        D4       D5
GG  95.08716  96.76798  92.42090  96.66944  82.8720
GV  88.07247 116.35255  90.30186  86.94521 106.3338
AR 116.84037  86.87947 117.27725 116.38535 110.7942

colnames(dat) <- c("a","b","c","d","e")
dat
           a         b         c         d        e
GG  95.08716  96.76798  92.42090  96.66944  82.8720
GV  88.07247 116.35255  90.30186  86.94521 106.3338
AR 116.84037  86.87947 117.27725 116.38535 110.7942

The right structure should look like this

dput(dat)
structure(list(D1 = c(95.0871563564004, 88.072473285318, 116.840370358282
), D2 = c(96.7679763688573, 116.352548635184, 86.8794749959586
), D3 = c(92.420897699915, 90.3018565270302, 117.277245773055
), D4 = c(96.6694428192656, 86.945205844304, 116.38535133643), 
    D5 = c(82.8720020998492, 106.333804799486, 110.794193100665
    )), class = "data.frame", row.names = c("GG", "GV", "AR"))

Upvotes: 1

Related Questions