简体   繁体   中英

Strange behaviour of names in lists

I assign names to a list of elements. The first one gets a string directly, the second one gets a name from an expression using paste. Why has the name in both cases a different format? Why has variable st below length=1 ?

rr<- list(5,9)
names(rr)[1]<- "xxx"
st<- paste("yy",toString(2))
names(rr)[2]<- st
rr
$xxx
[1] 5

$`yy 2`
[1] 9

rr$xxx
[1] 5

rr$yy2
NULL

is.character("xxx")
[1] TRUE
is.character(st)
[1] TRUE
length(st)
[1] 1

By default paste has a single space separator. If you want your variable to be called yy2 , you can create st either using the function paste0 or by explicitly using sep='' as the argument for paste :

#like this:
st<- paste("yy",toString(2), sep='')
# or this: 
st<- paste0("yy",toString(2))

Hope this helps.

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