Home > other >  Reshaping data table for plotting that has variable name and value int he same row
Reshaping data table for plotting that has variable name and value int he same row

Time:01-24

I'm in the process of building a Shiny app but need help with some data transformation in order to generate the plot. The SQL query and filter gives me the desired result, but the shape of the data is presenting a roadblock as everything is on a single row and the desired feature names are inside the columns. Currently the data table is shaped as follows

aName aValue bName bValue
FeatureNameA FeatureValueA FeatureNameB FeatureValueB

My goal is to have a Feature Importance plot but I don't want the feature labels to be the generic names (aName, bName), I'd like for them to be FeatureNameA and tied to FeatureValueA etc.

Thank you!

I tried melt() and t() but these are not getting the desired results. The desired result would be a reshaped table that looks like this:

Name Value
FeatureNameA FeatureValueA
FeatureNameB FeatureValueB

Hoping I can get help on writing a function to transform the table to get the desired result for a bar plot.

CodePudding user response:

library(data.table)
data.table::rbindlist(
  split.default(mydata, f = gsub("(.).*", "\\1", names(mydata))))

#           aName        aValue
# 1: FeatureNameA FeatureValueA
# 2: FeatureNameB FeatureValueB
  

CodePudding user response:

library(data.table)

melt(setDT(df1),,patterns('Name','Value'))

   variable       value1        value2
1:        1 FeatureNameA FeatureValueA
2:        2 FeatureNameB FeatureValueB

CodePudding user response:

We can use pivot_longer

library(tidyr)
pivot_longer(df1, cols = everything(), 
    names_to = c(".value"), names_pattern = ".(.*)")

-output

# A tibble: 2 × 2
  Name         Value        
  <chr>        <chr>        
1 FeatureNameA FeatureValueA
2 FeatureNameB FeatureValueB

Or with base R

 data.frame(Name = unlist(df1[c(TRUE, FALSE)]), 
    Value = unlist(df1[c(FALSE, TRUE)]))

data

df1 <- structure(list(aName = "FeatureNameA", aValue = "FeatureValueA", 
    bName = "FeatureNameB", bValue = "FeatureValueB"), 
class = "data.frame", row.names = c(NA, 
-1L))
  •  Tags:  
  • r
  • Related