简体   繁体   中英

R-programming: How can I change the variable names when I combine lists?

I have a data in this format:

x = c(list(a=1,b=2),list(a=5,b=6))

How can I change it to the following format?

x = list(a=1,b=2,a1=5,b1=6)

I am aware that I can achieve the above by using

names(x)[3:4]=c('a1','b1')

but it isn't effective as the the length of each lists vary in the data set that I have.

We can use make.unique and it works for all cases without doing any conversion

names(x) <- make.unique(names(x), sep="")
names(x)
#[1] "a"  "b"  "a1" "b1"

How about this...

as.list(as.data.frame(x))

$a
[1] 1

$b
[1] 2

$a.1
[1] 5

$b.1
[1] 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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