user1464667
user1464667

Reputation: 133

How do I preserve the data frame structure after using paste()?

> list1<-list("A1","A2")
> str(list1)
    List of 2
     $ : chr "A1"
     $ : chr "A2"
> list1<-paste(list1,"example",sep=" ")
> str(list1)
 chr [1:2] "A1 example" "A2 example"

I had wanted to preserve the list structure, but it seems like pasting new values into the list changes it.

How should I go about writing the code without affect the structure?

Thanks.

Upvotes: 2

Views: 139

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

Use [] to preserve the structure.

list1[] <- paste(list1,"example")

str(list1)
#List of 2
# $ : chr "A1 example"
# $ : chr "A2 example"

Upvotes: 2

Related Questions