0

I have data in type double as below:

               V1
2.policy       37

3.payments     51

4.supervisory  45

5.housing      22

6.inflation    11

V1 with all the number(Word frequency) is column 1 and serial number with the words is column 0, I want it in the form of a data table with words in one column and numbers in the corresponding column.

Can someone please let me know how to do that as general functions such as setDT are not working on type "double"

thanks

s_baldur
  • 29,441
  • 4
  • 36
  • 69
jalaj pathak
  • 67
  • 1
  • 8
  • What do you mean by 'column 0'? Indexing in R starts with 1. Do you mean the names? What is the output of str(object)? Please supply a minimal reproducible example [see here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Maybe youn can try `df <- data.frame(words = rownames(object), value = object$V1, stringsAsFactors=FALSE)` – dario Feb 09 '20 at 10:57
  • str(object) gives this Named num [1:797] 37 51 45 22 11 73 26 72 31 68 ... - attr(*, "names")= chr [1:797] "2.policy" "3.payments" "4.supervisory" "5.housing" ... – jalaj pathak Feb 09 '20 at 11:05
  • cannot produce a reproducible example here as the current double object is derived from a long process, using data.frame is not working, column 0 appears if i place the cursor on the names column header – jalaj pathak Feb 09 '20 at 11:08
  • Since your object is Named num see: https://stackoverflow.com/questions/16816032/convert-named-character-vector-to-data-frame – novica Feb 09 '20 at 11:17

1 Answers1

1

Not sure I really understand what you want, but if you have a named vector of type double:

myvec <- c(policy = 37, payments = 51, supervisory = 45, 
           housing = 22, inflation = 11)

You can convert it to a data table with:

dt <- data.table::as.data.table(myvec, .keep.rownames = "word")
meriops
  • 997
  • 7
  • 6
  • i tried the same before, it gives me only second column, that is of number s, how to extarct the words in the first column (or column 0 'rownames') – jalaj pathak Feb 09 '20 at 11:28
  • With the `.keep.rownames` argument, names are put into an actual colum (i.e., object `dt` has 2 columns). – meriops Feb 09 '20 at 12:18
  • used keep.rowname as true and then used stringr::str_extract(dt, "[a-z]+") to extract just the word in the dt as it was initially with word and serial number – jalaj pathak Feb 09 '20 at 15:42