将数据框转换为命名向量 [英] Turning a dataframe into a named vector
问题描述
我在dfm上应用了一个模型(wordscore).结果,我得到了一个输出对象,其类别为"textmodel_wordscores","textmodel","list".我对输出"wordscores"感兴趣,它是一个数字"对象(这是将 class 应用于该对象时所说的):
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.
推荐答案
您可以这样做:
with(df, setNames(Score, Words))
commitment progress implement decision message deficit
-0.9843452 -0.9831530 -0.9785868 -0.9777243 -0.9762919 -0.9752300
这篇关于将数据框转换为命名向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!