简体   繁体   中英

join two vectors with the = in R

I want to join two vectors with the = symbol, so that the result will be: "a"="z","b"="y","c"="x"

a<-c("a","b","c")
b<-c("z","y","x")
c<-rbind(a,b)

Assuming your desired output is as follows

c("a"="z","b"="y","c"="x")
#>   a   b   c 
#> "z" "y" "x"

this can be achieved using setNames with vectors a and b

a<-c("a","b","c")
b<-c("z","y","x")

setNames(b, a)
#>   a   b   c 
#> "z" "y" "x"
a<-c("a","b","c")
b<-c("z","y","x")

c <- paste0(a, "=", b)
c

[1] "a=z" "b=y" "c=x"

You may also use names<- ie

names(b) <- a
b

# a   b   c 
#"z" "y" "x" 

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