如何制作一个接收数组的函数

时间:2017-10-22 04:14:17

标签: arrays r function

我正在创建一个函数来接收颜色矢量和3x3x6数组。这将是一个rubiks立方体。我正在使用该函数来查看立方体是否已解决。我无法让函数使用传递给它的颜色向量和数组。

cube = function(color, cube){
  #Description:
  #Args:
  #Returns:
  array = array(color,cube)
  if(!dim(array)[1] == 3 | !dim(array)[2] == 3 | !dim(array)[3] == 6){
    print("Please enter a cube of 3 by 3 by 6")
  }
  if(!array[1] == 'red' | !array[2] == 'blue' | !array[3] == 'green' | 
  !array[4] == 'yellow' |     !array[5] == 'white' | !array[6] == 'orange'){
    print("Please enter a valid 6 colors for the cube")
  }
  #need more code here to check if the cube is solved
}

cube(c('red','blue','green','yellow','white','orange'), dim=c(3,3,6))

这是我得到的错误。

Error in cube(c('red','blue','green','yellow','white','orange'), dim = c(3, 3, 6)) : unused argument (dim = c(3, 3, 6))

由于

1 个答案:

答案 0 :(得分:0)

只需移除dim=并直接传递c(3,3,6)