Yamuna_dhungana
Yamuna_dhungana

Reputation: 663

How to assign name to R character object

I have a 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:

names(classes.group)
"Group"

Upvotes: 1

Views: 229

Answers (2)

akrun
akrun

Reputation: 887118

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

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 replicate the value 'Group' to the entire length

Upvotes: 1

rj-nirbhay
rj-nirbhay

Reputation: 679

use names to assign a name to object. in general

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

for your case:

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

Upvotes: 1

Related Questions