Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rename colnames Error object less than two dimensions

I am trying to rename colnames within a table which I have created.

This is the command I have used

table <- table(dataset$id)
colnames(table) <- c("id", "number_of_contacts")

When executing the following error occurs and I’m afraid I don’t know what to change to fix this and couldn’t find an answer so far.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error in `colnames<-`(`*tmp*`, value = c("id", "number_of_contacts" : 
  attempt to set 'colnames' on an object with less than two dimensions

Do I maybe need to change the variable class when calling for ‘class’ the following error occurs:

Error in table$Freq : $ operator is invalid for atomic vectors

I hope these informations are sufficient.
Below is the dput for the mentionned table:

structure(c(`8` = 1L, `11` = 6L, `25` = 1L, `28` = 1L, `34` = 2L, 
`47` = 3L, `53` = 4L, `54` = 1L, `62` = 3L, `65` = 5L, `69` = 3L, 
`73` = 3L, `75` = 2L, `79` = 1L, `88` = 1L, `91` = 1L, `92` = 5L, 
`93` = 4L, `96` = 7L, `98` = 1L, `100` = 2L, `104` = 5L, `105` = 1L, 
`106` = 1L, `113` = 3L, `114` = 3L, `121` = 4L, `124` = 5L, `130` = 1L, 
`140` = 3L, `143` = 1L, `152` = 1L, `155` = 2L, `157` = 3L, `158` = 7L, 
`162` = 3L, `167` = 2L, `171` = 7L, `173` = 6L, `175` = 4L, `178` = 3L, 
`183` = 7L, `192` = 2L, `197` = 1L, `201` = 1L, `203` = 2L, `210` = 1L, 
`213` = 3L, `216` = 7L, `221` = 1L, `224` = 1L, `227` = 3L, `229` = 2L, 
`230` = 2L, `231` = 1L, `239` = 3L, `242` = 2L, `244` = 1L, `246` = 4L, 
`255` = 3L, `256` = 3L, `257` = 7L, `259` = 1L, `260` = 6L, `267` = 2L, 
`276` = 2L, `280` = 4L, `281` = 3L, `283` = 1L, `287` = 2L, `294` = 6L, 
`300` = 6L, `305` = 6L, `308` = 6L, `310` = 1L, `312` = 5L, `314` = 3L, 
`318` = 1L, `321` = 2L, `323` = 1L, `327` = 1L, `328` = 3L, `335` = 4L, 
`338` = 1L, `343` = 2L, `346` = 4L, `351` = 6L, `354` = 1L, `356` = 3L, 
`364` = 1L, `372` = 4L, `376` = 5L, `379` = 6L, `388` = 4L, `397` = 4L, 
`398` = 4L, `403` = 2L, `406` = 7L, `411` = 3L, `412` = 1L, `416` = 2L, 
`417` = 1L, `418` = 7L, `419` = 3L, `422` = 1L, `424` = 4L, `429` = 4L, 
`436` = 1L, `439` = 1L, `443` = 3L, `450` = 4L, `455` = 1L, `458` = 3L, 
`459` = 1L, `461` = 2L, `467` = 4L, `469` = 2L, `470` = 4L, `477` = 1L, 
`488` = 5L, `490` = 2L, `496` = 5L, `499` = 1L, `500` = 1L, `512` = 2L, 
`514` = 2L, `517` = 1L, `519` = 3L, `522` = 1L, `531` = 2L, `532` = 4L, 
`538` = 2L, `540` = 3L, `542` = 3L, `548` = 1L, `549` = 2L, `553` = 1L, 
`554` = 2L, `555` = 1L, `557` = 1L, `563` = 5L, `570` = 5L, `572` = 1L, 
`573` = 4L, `576` = 2L, `578` = 1L, `589` = 1L, `596` = 1L, `600` = 1L, 
`601` = 2L, `603` = 4L, `605` = 2L, `606` = 1L, `609` = 1L, `615` = 1L, 
`616` = 1L, `619` = 4L, `625` = 5L, `627` = 1L, `631` = 1L, `635` = 2L, 
`637` = 1L, `642` = 4L, `643` = 3L, `647` = 3L, `651` = 1L, `654` = 6L, 
`669` = 3L, `670` = 2L, `673` = 2L, `676` = 1L, `683` = 4L, `688` = 5L, 
`696` = 4L, `700` = 2L, `701` = 3L, `705` = 1L, `706` = 1L, `718` = 1L, 
`724` = 3L, `728` = 6L, `729` = 3L, `731` = 2L, `732` = 1L, `734` = 6L, 
`743` = 2L, `744` = 1L, `747` = 6L, `750` = 4L, `752` = 2L, `754` = 1L, 
`757` = 1L, `760` = 1L, `763` = 3L, `765` = 6L, `771` = 3L, `774` = 3L, 
`788` = 5L, `789` = 2L, `792` = 1L, `798` = 1L, `800` = 1L, `801` = 2L, 
`802` = 1L, `811` = 4L, `813` = 2L, `814` = 6L, `816` = 1L, `817` = 3L, 
`831` = 2L, `832` = 4L, `834` = 1L, `836` = 4L, `838` = 5L, `839` = 7L, 
`846` = 4L, `847` = 3L, `852` = 6L, `856` = 6L, `862` = 1L, `863` = 6L, 
`864` = 1L, `866` = 1L, `871` = 4L, `880` = 3L, `883` = 6L, `889` = 1L, 
`896` = 1L, `899` = 4L, `903` = 2L, `905` = 1L, `906` = 2L, `907` = 1L, 
`908` = 6L, `910` = 3L, `915` = 1L, `918` = 3L, `925` = 6L, `931` = 2L, 
`932` = 6L, `933` = 7L, `936` = 2L, `938` = 6L, `940` = 2L, `943` = 2L, 
`944` = 1L, `952` = 5L, `956` = 1L, `959` = 3L, `962` = 3L, `967` = 1L, 
`971` = 3L, `972` = 1L, `974` = 6L, `989` = 1L, `990` = 1L, `995` = 2L, 
`1006` = 1L, `1007` = 3L, `1012` = 2L, `1017` = 3L, `1020` = 6L, 
`1026` = 3L, `1028` = 3L, `1033` = 6L, `1036` = 2L, `1055` = 1L, 
`1065` = 7L, `1069` = 1L, `1075` = 4L, `1077` = 4L, `1079` = 6L, 
`1081` = 3L, `1086` = 3L, `1087` = 2L, `1094` = 1L, `1107` = 1L, 
`1114` = 6L, `1116` = 1L, `1118` = 2L, `1121` = 5L, `1124` = 3L, 
`1134` = 3L, `1135` = 3L, `1139` = 1L, `1142` = 1L, `1143` = 2L, 
`1144` = 7L, `1148` = 4L, `1157` = 2L, `1162` = 5L, `1175` = 4L, 
`1180` = 3L, `1182` = 7L, `1184` = 1L, `1186` = 1L, `1188` = 3L, 
`1189` = 2L, `1192` = 1L, `1193` = 4L, `1202` = 2L, `1213` = 3L, 
`1214` = 2L, `1215` = 3L, `1217` = 2L, `1220` = 1L, `1227` = 5L, 
`1229` = 3L, `1230` = 4L, `1232` = 2L, `1235` = 1L, `1237` = 1L, 
`1240` = 3L, `1241` = 6L, `1255` = 4L, `1265` = 6L, `1267` = 5L, 
`1279` = 1L, `1280` = 4L, `1289` = 4L, `1299` = 3L, `1300` = 3L, 
`1305` = 4L, `1306` = 2L, `1311` = 1L, `1318` = 2L, `1329` = 2L, 
`1331` = 5L, `1336` = 1L, `1339` = 1L, `1342` = 4L, `1343` = 1L, 
`1345` = 1L, `1351` = 2L, `1352` = 1L, `1357` = 6L, `1365` = 2L, 
`1367` = 1L, `1370` = 5L, `1371` = 1L, `1375` = 1L, `1377` = 7L, 
`1378` = 2L, `1386` = 4L, `1397` = 1L, `1405` = 1L, `1406` = 2L, 
`1421` = 5L, `1423` = 1L, `1426` = 1L, `1429` = 1L, `1431` = 3L, 
`1432` = 1L, `1433` = 1L, `1436` = 4L, `1446` = 1L, `1447` = 4L, 
`1449` = 4L, `1450` = 3L, `1451` = 6L, `1458` = 2L, `1468` = 5L, 
`1470` = 7L, `1477` = 4L, `1478` = 5L, `1488` = 2L, `1491` = 1L, 
`1497` = 3L, `1501` = 6L, `1504` = 1L, `1506` = 2L, `1507` = 1L, 
`1508` = 1L, `1511` = 4L, `1517` = 3L, `1518` = 1L, `1519` = 1L, 
`1521` = 1L, `1530` = 1L, `1532` = 4L, `1533` = 6L, `1534` = 2L, 
`1537` = 1L, `1538` = 7L, `1539` = 2L, `1543` = 5L, `1555` = 4L, 
`1559` = 1L), .Dim = 388L, .Dimnames = structure(list(c("8", 
"11", "25", "28", "34", "47", "53", "54", "62", "65", "69", "73", 
"75", "79", "88", "91", "92", "93", "96", "98", "100", "104", 
"105", "106", "113", "114", "121", "124", "130", "140", "143", 
"152", "155", "157", "158", "162", "167", "171", "173", "175", 
"178", "183", "192", "197", "201", "203", "210", "213", "216", 
"221", "224", "227", "229", "230", "231", "239", "242", "244", 
"246", "255", "256", "257", "259", "260", "267", "276", "280", 
"281", "283", "287", "294", "300", "305", "308", "310", "312", 
"314", "318", "321", "323", "327", "328", "335", "338", "343", 
"346", "351", "354", "356", "364", "372", "376", "379", "388", 
"397", "398", "403", "406", "411", "412", "416", "417", "418", 
"419", "422", "424", "429", "436", "439", "443", "450", "455", 
"458", "459", "461", "467", "469", "470", "477", "488", "490", 
"496", "499", "500", "512", "514", "517", "519", "522", "531", 
"532", "538", "540", "542", "548", "549", "553", "554", "555", 
"557", "563", "570", "572", "573", "576", "578", "589", "596", 
"600", "601", "603", "605", "606", "609", "615", "616", "619", 
"625", "627", "631", "635", "637", "642", "643", "647", "651", 
"654", "669", "670", "673", "676", "683", "688", "696", "700", 
"701", "705", "706", "718", "724", "728", "729", "731", "732", 
"734", "743", "744", "747", "750", "752", "754", "757", "760", 
"763", "765", "771", "774", "788", "789", "792", "798", "800", 
"801", "802", "811", "813", "814", "816", "817", "831", "832", 
"834", "836", "838", "839", "846", "847", "852", "856", "862", 
"863", "864", "866", "871", "880", "883", "889", "896", "899", 
"903", "905", "906", "907", "908", "910", "915", "918", "925", 
"931", "932", "933", "936", "938", "940", "943", "944", "952", 
"956", "959", "962", "967", "971", "972", "974", "989", "990", 
"995", "1006", "1007", "1012", "1017", "1020", "1026", "1028", 
"1033", "1036", "1055", "1065", "1069", "1075", "1077", "1079", 
"1081", "1086", "1087", "1094", "1107", "1114", "1116", "1118", 
"1121", "1124", "1134", "1135", "1139", "1142", "1143", "1144", 
"1148", "1157", "1162", "1175", "1180", "1182", "1184", "1186", 
"1188", "1189", "1192", "1193", "1202", "1213", "1214", "1215", 
"1217", "1220", "1227", "1229", "1230", "1232", "1235", "1237", 
"1240", "1241", "1255", "1265", "1267", "1279", "1280", "1289", 
"1299", "1300", "1305", "1306", "1311", "1318", "1329", "1331", 
"1336", "1339", "1342", "1343", "1345", "1351", "1352", "1357", 
"1365", "1367", "1370", "1371", "1375", "1377", "1378", "1386", 
"1397", "1405", "1406", "1421", "1423", "1426", "1429", "1431", 
"1432", "1433", "1436", "1446", "1447", "1449", "1450", "1451", 
"1458", "1468", "1470", "1477", "1478", "1488", "1491", "1497", 
"1501", "1504", "1506", "1507", "1508", "1511", "1517", "1518", 
"1519", "1521", "1530", "1532", "1533", "1534", "1537", "1538", 
"1539", "1543", "1555", "1559")), .Names = ""), class = "table")
´´´


>Solution :

Since you only have one variable in your table(), I guess the dim of your table would be one dimensional, which is essential a named vector in this case.

If you want the assign colnames to it, use data.frame to change it to a dataframe, then from there you can use colnames.

Moreover, because table itself is the name of a function, you better not store object in this name, you can use for example table1.

table1 <- data.frame(table)
colnames(table1) <- c("id", "number_of_contacts")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading