Rollo99
Rollo99

Reputation: 1613

Turning a dataframe into a named vector

I applied a model (wordscore) on my dfm. As a result I got an object of output whose class is "textmodel_wordscores" "textmodel" "list". I am interested in the output "wordscores" which is a "numeric" object (this is what is said when class is applied to the object) that looks like this:

           extensive      fiscal_stimulus              measure           operation  automatic_stabiliser 
          0.96765485           0.98292581          -0.70247675           0.63275195           0.75272326 
                will 
         -0.04568423

Now I have a normal dataframe with two columns that looks like this:

df <- data.frame(Words = c("commitment", "progress", "implement", "decision", "message", "deficit"), Score = c(-0.984345245754151, -0.983153011766781, -0.978586816039627, -0.977724303324149, -0.976291875611652, -0.975229993562152))

    Words              Score
1 commitment -0.984345245754151
2   progress -0.983153011766781
3  implement -0.978586816039627
4   decision -0.977724303324149
5    message -0.976291875611652
6    deficit -0.975229993562152

I want this dataframe to become the same format as the one above. I tried to convert to a list but it keeps the double column framework.

Upvotes: 3

Views: 1018

Answers (2)

akrun
akrun

Reputation: 887751

We can also use deframe

library(tibble)
deframe(df)
#commitment   progress  implement   decision    message    deficit 
#-0.9843452 -0.9831530 -0.9785868 -0.9777243 -0.9762919 -0.9752300 

In base R, names<- can be used as well

with(df, `names<-`(Score, Words))

Upvotes: 1

tmfmnk
tmfmnk

Reputation: 40171

You can do:

with(df, setNames(Score, Words))

commitment   progress  implement   decision    message    deficit 
-0.9843452 -0.9831530 -0.9785868 -0.9777243 -0.9762919 -0.9752300 

Upvotes: 4

Related Questions