Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何创建包含阵列的插槽的S4阵列_R_S4 - Fatal编程技术网

R 如何创建包含阵列的插槽的S4阵列

R 如何创建包含阵列的插槽的S4阵列,r,s4,R,S4,如何创建将数组作为插槽的S4类?下面,我有一个示例类。我希望能够以这样一种方式构造两个人元素,每个元素都有适当的数组成员 下面的代码给出了以下错误:validObject.Object中的错误: 无效的类“person”对象:类person:Get类字符中的插槽子对象无效,应为或扩展类数组 setClass("person", representation(name="character", age="numeric", children = "array")) setMethod( f

如何创建将数组作为插槽的S4类?下面,我有一个示例类。我希望能够以这样一种方式构造两个人元素,每个元素都有适当的数组成员

下面的代码给出了以下错误:validObject.Object中的错误: 无效的类“person”对象:类person:Get类字符中的插槽子对象无效,应为或扩展类数组

setClass("person", representation(name="character", age="numeric", children = "array"))

setMethod(
  f = "[",
  signature="person",
  definition=function(x,i,j,...,drop=TRUE){ 
    initialize(x, name=x@name[i], age = x@age[i], children = x@children[i])
  }
)

setMethod(f = "length", signature = "person", definition = function(x){
  length(x@name)
})

setMethod(f = "dim", signature = "person", definition = function(x){
  length(x@name)
})

kids1 = as.array(c("Bob", "Joe", "Mary"))

person = new("person", name="John", age=40, children = kids1)

person@children[2]

kids2 = as.array(c("Steve", "Beth", "Kim"))

people = new("person", name=c("John", "Fred"), age=c(40, 20), children = as.array(c(kids1, kids2), dim = 2))

people[1]@age
people[2]@children[1]
将drop=FALSE添加到子窗口的子集中;这是数组子集的标准R规则的结果

setMethod(
  f = "[",
  signature="person",
  definition=function(x,i,j,...,drop=TRUE){ 
    initialize(x, name=x@name[i], age = x@age[i], 
      children = x@children[i,drop=FALSE])
  }
)
另外,我不确定您的as.array是否按照您的想法运行?正在忽略dim参数。

将drop=FALSE添加到子插槽的子集;这是数组子集的标准R规则的结果

setMethod(
  f = "[",
  signature="person",
  definition=function(x,i,j,...,drop=TRUE){ 
    initialize(x, name=x@name[i], age = x@age[i], 
      children = x@children[i,drop=FALSE])
  }
)
另外,我不确定您的as.array是否按照您的想法运行?dim参数被忽略。

您可能不希望@children成为数组。对于长度为1的dim属性,它本质上与向量相同,因此您无法区分不同的人的孩子。考虑把这个插槽改成一个列表。

setClass("person",
    representation(name="character", age="numeric", children = "list"))

person = new("person", name="John", age=40, children = list(kids1))
person@children

people = new("person", name=c("John", "Fred"), age=c(40, 20),
             children = list(kids1, kids2))
people[1]
您可能不希望@children成为数组。对于长度为1的dim属性,它本质上与向量相同,因此您无法区分不同的人的孩子。考虑把这个插槽改成一个列表。

setClass("person",
    representation(name="character", age="numeric", children = "list"))

person = new("person", name="John", age=40, children = list(kids1))
person@children

people = new("person", name=c("John", "Fred"), age=c(40, 20),
             children = list(kids1, kids2))
people[1]

洪的答案是有效的。我确实需要在[subset]函数中添加一个列表包装器。完成后,一切正常

kids1 = as.array(c("Bob", "Joe"))
kids2 = as.array(c("Steve", "Beth", "Kim"))

setClass("person", representation(name="character", age="numeric", children = "list"))

setMethod(
  f = "[",
  signature="person",
  definition=function(x,i,j,...,drop=TRUE){ 
    initialize(x, name=x@name[i], age = x@age[i], children = list(x@children[i]))
  }
)

people = new("person", name=c("John", "Fred"), age=c(40, 20), children = list(kids1, kids2))

people[1]@name
people[1]@age
people[1]@children

people[2]@name
people[2]@age
people[2]@children

Hong的答案是有效的。我确实需要在[subset]函数中添加一个列表包装器。完成后,一切都正常

kids1 = as.array(c("Bob", "Joe"))
kids2 = as.array(c("Steve", "Beth", "Kim"))

setClass("person", representation(name="character", age="numeric", children = "list"))

setMethod(
  f = "[",
  signature="person",
  definition=function(x,i,j,...,drop=TRUE){ 
    initialize(x, name=x@name[i], age = x@age[i], children = list(x@children[i]))
  }
)

people = new("person", name=c("John", "Fred"), age=c(40, 20), children = list(kids1, kids2))

people[1]@name
people[1]@age
people[1]@children

people[2]@name
people[2]@age
people[2]@children

使用列表非常有效。我还必须在subset运算符中添加包装器,以确保子插槽返回正确类型的变量。使用列表非常有效。我还必须在subset运算符中添加包装器,以确保子插槽返回正确类型的变量。我真的不明白为什么uld希望一个Person实例包含多个Person定义。在这种情况下,您的类应命名为Persons。您知道您可以将多个Person实例放在一个列表中,比如Persons\u list,然后使用Persons\u list[[i]]访问这些实例吗?我真的不明白为什么您希望一个Person实例包含多个Person定义。在这种情况下,您的类应该命名为Persons。您知道您可以将多个Person实例放在一个列表中,比如Persons_list,然后使用Persons_list[[I]]访问这些实例吗?