简体   繁体   English

如何为 R 字符 object 分配名称

[英]How to assign name to R character object

I have a dataframe:我有一个 dataframe:

inde.vars <- structure(list(Group = c("CUR", "CUR", "CUR", "CUR", "CUR", "CUR", 
"CUR", "CUR", "CUR", "CUR"), Subject = c("0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0"), Condition = c("L1", "L1", "L1", 
"L2", "L2", "L2", "L3", "L3", "L3", "L4"), Trial = c(1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L)), row.names = c(1L, 68L, 76L, 
151L, 226L, 301L, 376L, 451L, 464L, 539L), class = "data.frame")

classes.group <- inde.vars[,1]

What do I need to do to assign object name or some identifier, for example, "Group" for character object classes.group so I get someting like:我需要做什么来分配 object 名称或某些标识符,例如,字符 object classes.group的“组”,所以我得到类似:

names(classes.group)
"Group"

use names to assign a name to object.使用名称为 object 分配名称。 in general一般来说

names(object)<-c("ObjectDesiredName1","ObjectDesiredName2",..)

for your case:对于您的情况:

names(classes.group)<-"Group" or
names(classes.group)<-c("Group")

If we need an attribute then we can have如果我们需要一个属性,那么我们可以拥有

attr(classes.group, 'name') <- 'Group'
classes.group
#[1] "CUR" "CUR" "CUR" "CUR" "CUR" "CUR" "CUR" "CUR" "CUR" "CUR"
#attr(,"name")
#[1] "Group"

We can extract with attributes or attr我们可以用attributes或者attr提取

attr(classes.group, 'name')
#[1] "Group"

The issue with having a named vector when the vector length is greater than 1 is we need to rep licate the value 'Group' to the entire lengthvector length大于 1 时具有named vector的问题是我们需要将rep “组”复制到整个length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM