1

I have a data frame (read from RDS file) with 140 variables. I have subsetted 3 of them. But the subset has only one row with three column variables. I have to present it as a table and make a bar chart too. The subset data frame looks like this.

HomeCondn_Good    HomeCondn_Livabl    HomeCondn_Dilapdtd
       (dbl)            (dbl)              (dbl)
1      65.9             29.7                4.3

The reproducible example is as follows:

structure(list(HomeCondn_Good = 65.9, HomeCondn_Livabl = 29.7, 
                HomeCondn_Dilapdtd = 4.3), .Names = c("HomeCondn_Good", "HomeCondn_Livabl", 
                                                      "HomeCondn_Dilapdtd"), class = c("tbl_df", "data.frame"), row.names = c(NA, 
                                                                                                                              -1L))

I want to convert this into a table in the following format:

   Parameter           Proportion
1 HomeCondn_Good        65.9
2 HomeCondn_Livabl      29.7
3 HomeCondn_Dilapdtd     4.3

I tried reshape package and used melt function. (Assuming 'home' is the name of the object)

Home <- melt(Home)
names(Home)[1] <- "Parameter"
names(Home)[2] <- "Proportion"

I am getting the following warning:

No id variables; using all as measure variables

I am implementing this in Shiny, While I am getting the desired table output, this clearly impacts other components of the program which either not giving the output or just not rendering. Can someone help me understand this please?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
LeArNr
  • 635
  • 1
  • 6
  • 12

1 Answers1

0

We can use gather from tidyr

library(tidyr)
gather(df1, Parameter, Proportion)
#              Parameter Proportion
#               (fctr)      (dbl)
#1     HomeCondn_Good       65.9
#2   HomeCondn_Livabl       29.7
#3 HomeCondn_Dilapdtd        4.3
akrun
  • 874,273
  • 37
  • 540
  • 662
  • my apologies akrun.... i have pasted the wrong reproducible code which is after the `melt` exercise.... corrected now....the warning message happens when I try to melt. The original subset df don't have the option of Parameter and Proportion as they are renamed later. – LeArNr Dec 03 '15 at 13:04
  • @LeArNr I just copy/pasted the `dput` you posted, it is giving me the same output i.e. expected output – akrun Dec 03 '15 at 13:07
  • many thanks, sorry for the confusion again...don't know you can pass the variable names too in tidy.....it works...will it be possible to suppress the `(fctr)` and '(dbl)` messages... – LeArNr Dec 03 '15 at 13:13
  • @LeArNr It is the default structure for tbl_df. If you convert to `data.frame`, it will be gone `library(dplyr);gather(df1, Parameter, Proportion) %>% as.data.frame()` – akrun Dec 03 '15 at 13:14
  • many thanks again akrun...no need to remove the fctr and dbl tags...they don't appear in shiny. – LeArNr Dec 03 '15 at 13:19