【问题标题】:Which data type is closest to C struct in R哪种数据类型最接近 R 中的 C 结构
【发布时间】:2017-11-05 21:52:56
【问题描述】:

有什么方法可以在 R 中使用 C 结构,或者任何替代方法? 我正在搜索的是一种数据类型,它可以处理不同类型的数据,并以易于理解的方式授予访问权限。 R 列表已经这样做了,但内容的访问是通过索引[[ ]]。处理它似乎很乱,因为我必须记住第一个元素上的确切内容,秒等。c 结构是一个很好的例子,因为内容是使用. 运算符访问的,而程序员没有想想[[nth]] 保存了什么。

例如:

struct MyStuct{
    int powerLevel;
    int size;
} //create the data type

struct Mystruct variable;  //instantiate the object
variable.size  //access the content of the object

所以,我想要的是一种数据类型,用于将时间序列的点、其 SSE、组、用于 K 均值的 K 以及其他内容存储在单个变量中。我想到的最接近的是 C 结构。

【问题讨论】:

  • 您是否考虑过使用data.frame?此外,您可以将名称添加到列表中以简化访问。
  • 但是,dataframe 可以存储向量吗?
  • 试一试,亲自发现! help(data.frame); example(data.frame)。它是R中使用的主要数据结构。

标签: r struct abstract-data-type


【解决方案1】:

使用点 (.) 访问的 C 结构

struct MyStruct{
    int powerLevel;
    int size;
} variable;  
variable.size; 

使用禁止的 S ($) 访问的类似 R 代码

variable = list(
     powerlevel = 0,
     size = 0
)
variable$size

【讨论】:

    【解决方案2】:

    你可能想看看使用 R::setClass

    本质上,这允许您创建一个类定义并返回一个生成器函数以从该类创建对象。就个人而言,我发现它在您描述的上下文中非常有用。要取消引用结构组件,您可以使用@,使用下面的示例CStructure@powerLevel 将返回5

    作为替代方案,可以考虑使用数据框,但是,数据框方法不会创建独立的类模板。它还要求所有条目的长度相同。数据框对称,您的数据可能不是。

    请参阅示例错误参考:数据帧必须是对称的在下面使用setClass 的示例详细说明。也就是说,两者都是选项。

    希望这个例子对你有用。

    示例

    setClass(
      "CStruct",
      slots = list(
        powerLevel = "numeric",
        size = "numeric"
      )
    )
    
    CStructure <- new("CStruct", powerLevel =5, size=10)
    CStructure
    str(CStructure)
    CStructure@powerLevel
    CStructure@size
    

    输出:

    > setClass(
    +   "CStruct",
    +   slots = list(
    +     powerLevel = "numeric",
    +     size = "numeric"
    +   )
    + )
    > 
    > CStructure <- new("CStruct", powerLevel =5, size=10)
    > CStructure
    An object of class "CStruct"
    Slot "powerLevel":
    [1] 5
    
    Slot "size":
    [1] 10
    
    > str(CStructure)
    Formal class 'CStruct' [package ".GlobalEnv"] with 2 slots
      ..@ powerLevel: num 5
      ..@ size      : num 10
    > CStructure@powerLevel
    [1] 5
    > CStructure@size
    [1] 10
    > 
    

    数据框必须是对称的

    > n = c(2, 3, 5)
    > s = c("aa", "bb")
    > b = c(TRUE, FALSE, TRUE)
    > df = data.frame(n, s, b)
    Error in data.frame(n, s, b) : 
      arguments imply differing number of rows: 3, 2
    

    CStruct 替代方案

    setClass(
      "CStruct2",
      slots = list(
       n = "numeric",
       s = "character",
       b = "logical"
      )
    )
    
    CStructure2 <- new("CStruct2", n = c(2, 3, 5), 
                      s = c("aa", "bb"), 
                      b = c(TRUE, FALSE, TRUE) )
    str(CStructure2)
    CStructure2@n
    CStructure2@s
    CStructure2@b
    

    输出:

    > setClass(
    +   "CStruct2",
    +   slots = list(
    +    n = "numeric",
    +    s = "character",
    +    b = "logical"
    +   )
    + )
    > 
    > CStructure2 <- new("CStruct2", n = c(2, 3, 5), 
    +                   s = c("aa", "bb"), 
    +                   b = c(TRUE, FALSE, TRUE) )
    > str(CStructure2)
    Formal class 'CStruct2' [package ".GlobalEnv"] with 3 slots
      ..@ n: num [1:3] 2 3 5
      ..@ s: chr [1:2] "aa" "bb"
      ..@ b: logi [1:3] TRUE FALSE TRUE    
    > CStructure2@n
    
    [1] 2 3 5
    > CStructure2@s
    [1] "aa" "bb"
    > CStructure2@b
    [1]  TRUE FALSE  TRUE
    

    【讨论】:

      【解决方案3】:
      > n = c(2, 3, 5) 
      > s = c("aa", "bb", "cc") 
      > b = c(TRUE, FALSE, TRUE) 
      > df = data.frame(n, s, b) 
      

      数据框可以存储向量。

      【讨论】:

      • 这似乎解决了这个问题。我不得不承认我从来没有用这种眼睛看到过 data.frame 哈哈。我会稍等片刻,看看是否有人提供更花哨的东西。不过,谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多