R datamembers and Attributes

2019-07-30 04:37发布

I am very new to R and am not sure about the proper language when referring to datamembers and attributes of an object. I have an object-oriented programming language background in Java so I am probably referring to datamembers/attributes in a Java set of mind. Anyway, suppose I have a matrix matClust1 and I have done the following:

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

attr(matClust1, "clustID") <- ids

I think of the above as setting an attribute/datamember of each row vector in matClust1 to "exp". This is because ids is created to have as many entries as there are rows in matClust1. So, I would like to be able to access this attribute/datamember by code like the following:

matClust1.clustID[2]            #get the clustID of row vector in matClust1

Apparently the . operator does not do this nor does the $ operator.

  1. How can I achieve this functionality in R?

EDIT: I already have the attribute set for rownames and what I am really looking for is another variable to work like rownames.

1条回答
地球回转人心会变
2楼-- · 2019-07-30 05:11

You probably want a data.frame or data.table instead.

E.g.

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)
查看更多
登录 后发表回答