Reputation: 47
I have a matrix (which I have called "y") with 30 rows and 4 columns. Right now, the column names are NULL. I am trying to change the individual column names, but it is not quite working.
I have tried renaming each column individually (after creating the matrix, that is, please see below).
y <- t(replicate(30, diff(c(0, sort(runif(3)), 100))))
names(y)[1] <- "very small"
names(y)[2] <- "small"
names(y)[3] <- "medium"
names(y)[4] <- "large"
Here is my matrix, but the headers did not work:
[,1] [,2] [,3] [,4]
[1,] 0.160791460 0.19036607 0.12580291 99.52304
[2,] 0.494404672 0.37335411 0.12121969 99.01102
[3,] 0.322312759 0.07680743 0.13229986 99.46858
[4,] 0.320615717 0.02913797 0.48735808 99.16289
[5,] 0.250489308 0.20835271 0.49417666 99.04698
[6,] 0.065639576 0.62845967 0.11742292 99.18848
[7,] 0.318023945 0.09396768 0.25217427 99.33583
[8,] 0.109697064 0.26258440 0.43483501 99.19288
[9,] 0.438948497 0.23642842 0.31614101 99.00848
[10,] 0.061270783 0.44413492 0.05874163 99.43585
[11,] 0.587190304 0.07693372 0.07609361 99.25978
[12,] 0.097498817 0.05573392 0.34773439 99.49903
[13,] 0.418862582 0.04924732 0.25642872 99.27546
[14,] 0.313116374 0.02291056 0.43163056 99.23234
[15,] 0.167363657 0.28980591 0.10015846 99.44267
[16,] 0.003417506 0.38710120 0.34930292 99.26018
[17,] 0.232000787 0.30794160 0.06402202 99.39604
[18,] 0.233561779 0.35227333 0.36642543 99.04774
[19,] 0.396694646 0.11903936 0.41165674 99.07261
[20,] 0.147861399 0.13515116 0.59463398 99.12235
[21,] 0.469082454 0.03757156 0.29131755 99.20203
[22,] 0.008113136 0.53687528 0.35030648 99.10471
[23,] 0.097810979 0.24581902 0.07533917 99.58103
[24,] 0.666495166 0.09177066 0.20840372 99.03333
[25,] 0.198825197 0.03246302 0.14705370 99.62166
[26,] 0.460175968 0.17809558 0.34293910 99.01879
[27,] 0.349538516 0.50323914 0.13838036 99.00884
[28,] 0.498382490 0.18309442 0.17280276 99.14572
[29,] 0.817413253 0.08962883 0.02642045 99.06654
[30,] 0.139955595 0.30773060 0.22649081 99.32582
Instead, directly under my matrix, I get something like this:
attr(,"names")
[1] "very small" "small" "medium" "large" NA NA
NA NA NA NA
[11] NA NA NA NA NA NA
NA NA NA NA
[21] NA NA NA NA NA NA
NA NA NA NA
[31] NA NA NA NA NA NA
NA NA NA NA
[41] NA NA NA NA NA NA
NA NA NA NA
[51] NA NA NA NA NA NA
NA NA NA NA
[61] NA NA NA NA NA NA
NA NA NA NA
[71] NA NA NA NA NA NA
NA NA NA NA
[81] NA NA NA NA NA NA
NA NA NA NA
[91] NA NA NA NA NA NA
NA NA NA NA
[101] NA NA NA NA NA NA
NA NA NA NA
[111] NA NA NA NA NA NA
NA NA NA NA
Any help would be appreicated. Thanks.
Upvotes: 1
Views: 669
Reputation: 624
Try using colnames
y <- t(replicate(30, diff(c(0, sort(runif(3)), 100))))
colnames(y) <- c("very small", "small", "medium", "large")
Which gives the following output:
very small small medium large
[1,] 0.596546432 0.115485521 0.09401027 99.19396
[2,] 0.118521670 0.694866442 0.16535686 99.02126
[3,] 0.421295392 0.002795051 0.03821284 99.53770
[4,] 0.202813028 0.269167296 0.21997193 99.30805
[5,] 0.008274178 0.222862752 0.56131816 99.20754
[6,] 0.031685216 0.303549471 0.04934352 99.61542
[7,] 0.319768410 0.022998874 0.25970746 99.39753
[8,] 0.087382152 0.609509549 0.09651936 99.20659
[9,] 0.773647428 0.086955708 0.02798129 99.11142
[10,] 0.331411455 0.368387836 0.13918296 99.16102
[11,] 0.131787972 0.463235363 0.22744175 99.17753
[12,] 0.293173234 0.345394548 0.23810145 99.12333
[13,] 0.153707973 0.074851698 0.44902831 99.32241
[14,] 0.153896874 0.017483677 0.05351859 99.77510
[15,] 0.182090415 0.502133689 0.29637249 99.01940
[16,] 0.413675049 0.082363683 0.26351923 99.24044
[17,] 0.006476240 0.001355163 0.39702371 99.59514
[18,] 0.558440146 0.177935100 0.13974163 99.12388
[19,] 0.066856618 0.095454829 0.22950901 99.60818
[20,] 0.050508366 0.202233682 0.41133537 99.33592
[21,] 0.152118187 0.373801691 0.01678485 99.45730
[22,] 0.545999638 0.070524028 0.03855460 99.34492
[23,] 0.216591213 0.171351144 0.41215730 99.19990
[24,] 0.214317867 0.388471707 0.24408460 99.15313
[25,] 0.134022010 0.380754224 0.29407828 99.19115
[26,] 0.071872900 0.127088718 0.39654603 99.40449
[27,] 0.105251258 0.554369684 0.23365911 99.10672
[28,] 0.233648165 0.224023261 0.19177330 99.35056
[29,] 0.649248827 0.139018328 0.14818464 99.06355
[30,] 0.090693591 0.034686614 0.10709328 99.76753
names
in this context tries to assign a name to each item within the matrix, rather than each column (as you would find with a dataframe). This is because names
assigns a name to each element of the object i.e. as many names as the length
of the matrix, in this case 120.
Dataframes, however, are in fact stored as lists, where each element of the list represents a column. Therefore, a similar dataframe would have length
of 4, and names
would just try to assign 4 names, one per column.
colnames
, however assigns as many names as the length of the second dimension i.e. the number of columns. Since dim(y)
gives 30 4
whether y
is a matrix or a dataframe, either way it'll assign 4 names, one per column.
Upvotes: 3