Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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数据成员和属性_R_Oop_Class_Attributes_Datamember - Fatal编程技术网

R数据成员和属性

R数据成员和属性,r,oop,class,attributes,datamember,R,Oop,Class,Attributes,Datamember,我对R非常陌生,不确定在引用对象的数据成员和属性时使用什么语言。我有Java的面向对象编程语言背景,所以我可能是指Java思维中的datamembers/attributes。无论如何,假设我有一个矩阵matClust1,我做了以下工作: ids = vector() for(i in 1:size) #size is the number of rows in matClust1 { ids = c(ids, "exp") } attr(matClust1, "

我对R非常陌生,不确定在引用对象的数据成员和属性时使用什么语言。我有Java的面向对象编程语言背景,所以我可能是指Java思维中的datamembers/attributes。无论如何,假设我有一个矩阵
matClust1
,我做了以下工作:

ids = vector()
for(i in 1:size)              #size is the number of rows in matClust1
{
  ids = c(ids, "exp")
}

attr(matClust1, "clustID") <- ids
matClust1.clustID[2]            #get the clustID of row vector in matClust1
显然,
运算符不执行此操作,
$
运算符也不执行此操作

  • 如何在R中实现此功能

  • 编辑:我已经为
    行名
    设置了属性,我真正想要的是另一个类似
    行名
    的变量

    您可能需要一个
    数据.frame
    数据.table

    例如


    除了一个可能涉及使用
    rownames()
    的特定解决方案之外,如果您有Java背景,那么您一定要阅读,尤其是圆圈2+3。这会帮你省去很多痛苦。只是一个简单的提示-
    不是
    R
    中的操作员。这只是另一个角色。当你看到
    时,想想
    (下划线)@RicardoSaporta:这只是部分正确。事实仅限于数据对象命名。使用S3调度系统的函数命名依赖于具有特殊意义的“.”。但对于OP来说,这是一种函数分派方法,不能很好地转化为Java训练的思维过程。根据您的编辑,我建议您要么切换到数据帧,要么使用更完善的对象类型结构,如S4或引用类?
    df = data.frame(matClust1)
    
    # create a new column and assign whatever to it:
    df$clustID = "exp"
    
    # use it however you like
    df$someOtherColumn = paste(1:dim(df)[1], df$clustID)