Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

r - Can you change the name of an element on an array without trying to set the names of the entire array?

I was recently surprised to find that the following didn't work:

output<-array(1:10)
names(output[1])<-"Foo"

As far as I can tell, this does not changes the name of output[1]. Is it possible to modify the names of the individual elements of an array like this? Or am I forced to write code that modifies the entire array, like names(output)<-c("foo",rep(NA,9))?

question from:https://stackoverflow.com/questions/66065541/can-you-change-the-name-of-an-element-on-an-array-without-trying-to-set-the-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You want to change the first name of an element, so [1] goes outside names(output):

output<-array(1:10)
names(output)[1]<-"Foo"
names(output)
#>  [1] "Foo" NA    NA    NA    NA    NA    NA    NA    NA    NA

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...