ACE
ACE

Reputation: 335

Finding column names of a data frame using $ operator

Is there a way to find the column names of a data frame using $ operator? I have written a function to plot x data and variable number of y data using plotly. I am working with a dataset with 20 columns. I need to subset a variable number of columns and plot using plotly. I wrote the following two functions:

subset_variable_cols()

for subsetting arbitrary number of columns from dataset

 subset_variable_cols <- function(df, ...){
      
      x <- list(...)
      
      sub <- subset(df, select = c(...))
      return(sub)
    }

and

 plotly_variable_y()

for plotting the variables specified by the user, where the first argument is x.

 plotly_variable_y <- function(vars){
  
     uu <- subset_variable_cols(df, vars)
    setDT(uu)   longDF <- melt(uu, id.vars = "Time")
     plot_ly(longDF, type = "scatter", mode = "lines", x = ~Time, y = ~value, split = ~variable) }

The data frame is as follows

df <- data.frame(x = 1:100, y1 = rnorm(100), y2 = rnorm(100), .... ,y20 = rnorm(100))

plotly_variable_y(x, y1, y6, y10) will plot y1, y6 and y10 in the y axis with common shared x

The issue is the names of the actual data frame that are quite big with tags, so I would like to keep them as it is. I was trying to find an easy way to extract the names from df$y1, df$y6, df$y10, etc since the user can call the function easily using Rstudio's ability to autofill the names after typing 'df$'. Any ideas how to go about it?

Upvotes: 0

Views: 67

Answers (1)

jay.sf
jay.sf

Reputation: 73802

Try this

library(plotly); library(data.table)

plotly_variable_y <- function(...) {
  subset_variable_cols <- function(...) {
    x <- cbind.data.frame(...)
    setNames(x, gsub('.*\\$', '', names(x)))
  }
  uu <- subset_variable_cols(...)
  setDT(uu) 
  longDF <- melt(uu, id.vars="x")
  plot_ly(longDF, type="scatter", mode="lines", x=~x, y=~value, split=~variable) 
}

plotly_variable_y(df$x, df$y1, df$y2)

enter image description here


Data:

set.seed(42)
df <- data.frame(x=1:10, y1=rnorm(10), y2=rnorm(10), y20=rnorm(10))

Upvotes: 0

Related Questions