简体   繁体   中英

How to reshape this dataframe?

I have the following dataframe where words took the place of default row numbers (1,2,3, etc.):

                       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); 2) keep only those words that have a score bigger or equal 0. I tried a lot of solution but I failed. 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).

Is there anyone who can help me with that? Thanks so much!

Here is an option with 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(newcol = row.names(df1), Score = df1$Score), Score >=0)

Or using tidyverse

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

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