0

I have a large data set in which I have to search for specific codes depending on what i want. For example, chemotherapy is coded by ~40 codes, that can appear in any of 40 columns called (diag1, diag2, etc).

I am in the process of writing a function that produces plots depending on what I want to show. I thought it would be good to specify what I want to plot in a input data frame. Thus, for example, in case I only want to plot chemotherapy events for patients, I would have a data frame like this:

Dataframe name: Style
 Name   SearchIn                                codes         PlotAs  PlotColour
 Chemo  data[substr(names(data),1,4)=="diag"]  1,2,3,4,5,6     |       red

I already have a function that searches for codes in specific parts of the data frame and flags the events of interest. What i cannot do, and need your help with, is referring to a data frame (Style$SearchIn[1]) using codes in a data frame as above.

> Style$SearchIn[1]
[1] data[substr(names(data),1,4)=="diag"]
Levels:  data[substr(names(data),1,4)=="diag"] 

I thought perhaps get() would work, but I cant get it to work:

> get(Style$SearchIn[1])
Error in get(vars$SearchIn[1]) : invalid first argument
enter code here

or

> get(as.character(Style$SearchIn[1]))
Error in get(as.character(Style$SearchIn[1])) :   
object 'data[substr(names(data),1,5)=="TDIAG"]' not found

Obviously, running data[substr(names(data),1,5)=="TDIAG"] works.

Example:

  library(survival)
  ex <- data.frame(SearchIn="lung[substr(names(lung),1,2) == 'ph']")

  lung[substr(names(lung),1,2) == 'ph'] #works
  get(ex$SearchIn[1]) # does not work
1
  • 1
    @TimBiegeleisen I disagree, I don't think this question is poorly written. The OP is just wrestling with a design issue, and trying to use data.frames and dynamic logic in inadvisable ways. I think the overall intention of the question is clear. Commented May 26, 2015 at 2:42

2 Answers 2

3

It is not a good idea to store R code in strings and then try to eval them when needed; there are nearly always better solutions for dynamic logic, such as lambdas.

I would recommend using a list to store the plot specification, rather than a data.frame. This would allow you to include a function as one of the list's components which could take the input data and return a subset of it for plotting.

For example:

library(survival);

plotFromSpec <- function(data,spec) {
    filteredData <- spec$filter(data);
    ## ... draw a plot from filteredData and other stuff in spec ...
};

spec <- list(
    Name='Chemo',
    filter=function(data) data[,substr(names(data),1,2)=='ph'],
    Codes=c(1,2,3,4,5,6),
    PlotAs='|',
    PlotColour='red'
);
plotFromSpec(lung,spec);

If you want to store multiple specifications, you could create a list of lists.

Sign up to request clarification or add additional context in comments.

2 Comments

This is an interesting idea... The potential problem with this is that I can have dozens of things to include, and it might vary from 1 project to another. I guess i can have a list of each of the items, and combine lists in order to make the complete specs list...
EDIT (10 mins later) mmmm I am struggling how to use this approach with multiple specs (e.g chemo, surgery, radiotherapy)
2

Have you tried using quote()

I'm not entirely sure what you want but maybe you could store the things you're trying to get() like

quote(data[substr(names(data),1,4)=="diag"])

and then use eval()

eval(quote(data[substr(names(data),1,4)=="diag"]), list(data=data))

For example,

dat <- data.frame("diag1"=1:10, "diag2"=1:10, "other"=1:10)
Style <- list(SearchIn=c(quote(data[substr(names(data),1,4)=="diag"]), quote("Other stuff")))

> head(eval(Style$SearchIn[[1]], list(data=dat)))
  diag1 diag2
1     1     1
2     2     2
3     3     3
4     4     4
5     5     5
6     6     6

4 Comments

Its not 'data[substr(names(data),1,4)=="diag"]' that doesnt work, it is Style$SearchIn[1], that doesnt work. quote(Style$SearchIn[1]) doesn't help me.Thnx
@Luc You would need to quote the data[...] and eval() the Style$SearchIn[1]
Sorry, I don't fully understand. Can you write it out?
@Luc I added an example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.