kin182
kin182

Reputation: 403

How to concatenate the row names of a data frame with a special character?

I would like to concatenate the row names of a data frame with a "+" to return a string. For example

head(USArrests)

           Murder Assault UrbanPop Rape
Alabama      13.2     236       58 21.2
Alaska       10.0     263       48 44.5
Arizona       8.1     294       80 31.0
Arkansas      8.8     190       50 19.5
California    9.0     276       91 40.6
Colorado      7.9     204       78 38.7

My goal is to get

goal = Alabama+Alaska+Arizona+Arkansas+California+Colorado+...

I tried

goal = paste0(rownames(USArrests), sep="+")

head(goal)
[1] "Alabama+"    "Alaska+"     "Arizona+"    "Arkansas+"   "California+"     "Colorado+"

I am relatively new to R and am lost on how to get the one I wanted. Could someone help me? Thanks!

Upvotes: 0

Views: 986

Answers (1)

Vitalijs
Vitalijs

Reputation: 950

paste(rownames(USArrests), collapse="+")

Upvotes: 2

Related Questions