Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/Questions/The complete guide to the ''error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent'' issue.
Next
Answered
Freya Morton
  • 8
Freya Morton
Asked: May 18, 20222022-05-18T21:47:35+00:00 2022-05-18T21:47:35+00:00In: r

The complete guide to the ”error in dimnames(x) <- dn : length of ‘dimnames’ [2] not equal to array extent” issue.

  • 8

I am tired of fixing the problem: error in dimnames(x) <- dn : length of ‘dimnames’ [2] not equal to array extent in the r; even if I get the reference from another forum, it still returns an error:

Error in `colnames<-`(`*tmp*`, value = c(6, 7, 8, 9, 10, 11, 12)) : 
  length of 'dimnames' [2] not equal to array extent

To identify the problem, I will show you the detail here:

layout(matrix(c(1, 1, 2, 2, 3, 3, 4, 4, 5), ncol=1))
 
 # main plots
 par(mai=rep(0.5, 4))
 
 fcm <-c(14.0,14.1,13.0,14.2,14.7,13.8,14.0)
 gk <-c(12.1,12.5,12.2,12.0,11.5,12.0,11.4)
 gg <-c(14.0,14.1,13.3,12.8,12.0,12.2,12.0)
 data1 <- rbind(fcm,gk,gg)
 colnames(data1) <- c(6,7,8,9,10,11,12)
 
 fcm <-c(2.65,2.55,2.4,2.45,2.45,2.5,2.45)
 gk <-c(2.45,2.55,2.4,2.3,2.2,2.35,2.1)
 gg <-c(2.6,2.65,2.5,2.35,2.4,2.4,2.2)
 data2 <- rbind(fcm,gk,gg)
 colnames(data2) <- c(6,7,8,9,10,11,12)
 
 fcm <-c(8.8,6.5,6.6,8.2,8.0,8.4,9.0)
 gk <-c(12.7,11.0,11.1,10.5,10.7,10.0,9.5)
 gg <-c(2.1,2.1,1.8,2.0,2.0,1.9,1.8)
 data3 <- rbind(fcm,gk,gg)
 colnames(data3) <- c(6,7,8,9,10,11,12)
 
 fcm <-c(0.47,0.53,0.45,0.39,0.40,0.47,0.48)
 gk <-c(0.45,0.51,0.34,0.40,0.42,0.42,0.44)
 data4 <- rbind(fcm,gk)
 colnames(data4) <- c(6,7,8,9,10,11,12)
 
 barplot(as.matrix(data1),ylim=c(0,20),main="P wave",
  xlab="number of clusters", ylab="traveltime rms(ms)",
  col=c("red", "black", "green"), beside=TRUE)
 barplot(as.matrix(data2),ylim=c(0,2),main="MT",
  xlab="number of clusters", ylab="MT functions",
  col=c("red", "black", "green"), beside=TRUE)
 barplot(as.matrix(data3),ylim=c(0,20),main="XBI",
  xlab="number of clusters", ylab="index value",
  col=c("red", "black", "green"), beside=TRUE)
 barplot(as.matrix(data4),ylim=c(0,0.6),main="NCE",
  xlab="number of clusters", ylab="index value",
  col=c("red", "black"), beside=TRUE)
 
 par(mai=c(0,0,0,0))
 plot.new()
 legend(legend = c("fcm","gk","gg"), fill = c( "red", "black", "green"), 
  "center", horiz=TRUE)

How do I do that? Could you support me in improving this problem?

length of dimnames not equal to array extent
  • 1 1 Answer
  • 109 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-28T02:57:58+00:00Added an answer on June 28, 2022 at 2:57 am

    The cause:

    You have got this error because you are having 8 columns but you only provide 7 collumn names for them. It shows up obviously in your program:

    > length(fcm)
    [1] 8

    Or

    > ncol(data1)
    [1] 8

    Even the matrix is shown:

    > data1
    # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
    #fcm 14.0 14.1 13.0 14 2.0 14.7 13.8 14.0
    #gk 12.1 12.5 12.2 12 0.0 11.5 12.0 11.4
    #gg 14.0 14.1 13.0 3 12.8 12.0 12.2 12.0

    At the time

    > length( c(6, 7, 8, 9, 10, 11, 12))
    [1] 7

    Solution:

    To solve this error, you can try:

    colnames(data1) <- c(6:13)

    Let’s do as the following command for your data:

    fcm <-c(14.0,14.1,13.0,14,2,14.7,13.8,14.0)
    gk <-c(12.1,12.5,12.2,12,0,11.5,12.0,11.4)
    gg <-c(14.0,14.1,13,3,12.8,12.0,12.2,12.0)
    data1 <- rbind(fcm,gk,gg)
    colnames(data1) <- c(6:13)

    Then, it gives the result without any error:

    > data1
    6 7 8 9 10 11 12 13
    fcm 14.0 14.1 13.0 14 2.0 14.7 13.8 14.0
    gk 12.1 12.5 12.2 12 0.0 11.5 12.0 11.4
    gg 14.0 14.1 13.0 3 12.8 12.0 12.2 12.0

    Your error has been fixed successfully.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.