简体   繁体   English

如何重塑此数据框?

[英]How to reshape this dataframe?

I have the following dataframe where words took the place of default row numbers (1,2,3, etc.):我有以下数据框,其中单词代替了默认行号(1、2、3 等):

                       Score
commitment            -0.9843452
progress              -0.9831530
implement             -0.9785868
decision              -0.9777243
message               -0.9762919
deficit               -0.9752300
invest                0.9929340
multiplier            0.9940889
fiscal_capacity       0.9940889
public_investment     0.9949193
aggregate_demand      0.9955452
space                 0.9960338

I want to achieve two things: 1) the "column" of words to become a proper column (the second column of the dataframe);我想实现两件事:1)单词的“列”成为适当的列(数据框的第二列); 2) keep only those words that have a score bigger or equal 0. I tried a lot of solution but I failed. 2) 只保留那些得分大于或等于 0 的单词。我尝试了很多解决方案,但都失败了。 Unfortunately, I don't know how to create a dfm with words instead of numbers so I can't provide you with code (I derived it from list in my real-life scenario).不幸的是,我不知道如何用单词而不是数字创建 dfm,所以我无法为您提供代码(我从现实生活中的列表中推导出来)。

Is there anyone who can help me with that?有没有人可以帮我解决这个问题? Thanks so much!非常感谢!

Here is an option with base R .这是一个带有base R的选项。 Create a data.frame from the row names of the dataset and subset the rows based on the values of 'Score'从数据subset的行名称创建一个data.frame ,并根据“Score”的值对行进行subset

subset(data.frame(newcol = row.names(df1), Score = df1$Score), Score >=0)

Or using tidyverse或者使用tidyverse

library(dplyr)
library(tibble)
rownames_to_column(df1, 'newcol')  %>%
     filter(Score >= 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM