Search code examples
rdata-structurescompiler-errorssyntax-errorcustom-data-type

Error in array(c(fruits, cost), dim = c(3, 2, 1), dimnames = list(shop_name, : length of 'dimnames' [2] not equal to array extent


fruits = c('Apple', 'Banana', 'Grapes')

cost = c(20,10,30)
  
shop_name = c("Fruits guy", "Champa seller', "Bad mango"")

price = c("cost of fruits")

market_name = c("Jhoom Market")
  
  
shops_info = array(c(fruits, cost), dim=c(3,2,1), dimnames=list(shop_name, price, market_name)) 
  
print(shops_info)

Solution

  • The problem is that your second dimension is of size two. However, the name of the dimension pricehas only one entry. R is expecting it to have 2 entries. This would work:

    fruits = c('Apple', 'Banana', 'Grapes')
    cost = c(20,10,30)
    
    shop_name = c("Fruits guy", "Champa seller", "Bad mango")
    price = c("cost of fruits", "Entry needed") #adding fictional name
    market_name = c("Jhoom Market")
    
    
    shops_info = array(c(fruits, cost), dim=c(3,2,1), dimnames=list(shop_name, price, market_name)) 
    
    print(shops_info)
    , , Jhoom Market
    
                  cost of fruits Entry needed
    Fruits guy    "Apple"        "20"        
    Champa seller "Banana"       "10"        
    Bad mango     "Grapes"       "30"