I am trying to obtain certain cutoff value in multiple variables (columns 3: 69) of a data frame (name = Data). This is how data looks like What I wanted is to loop over the columns 3:69 (i.e. their values/numbers of course), to obtain the optimal cutoff value that could discriminate between stauses (Lables) of patients using the Youden method. This is the code I am using:
for (i in 1:ncol(Data)) {optimal.cutpoints(X =i, status = 'Label',
tag.health = 'Control',
method = 'Youden', data = Data)}
I am not sure if i in 1:ncol
would be the right thing to put in here ? When I run the code the following error appear
Error: Not all needed variables are supplied in 'data'.
I do understand that I R did not find the variables within the defined df (Data). but how can I formulate the for in loop to allow R applying the cutoff function for all columns at once especially given that the cutoff needs an X valu, which I assume should be the predefined i ?
Thanks
Dear Jeremy, Thanks. What I could not understand why you wrote it like : cutpoint_results[[col_name]] ? Is it because it is a list results ? Also if I have a list of those cutoff (cut.list), how can I download it as an excel table ?
Thanks
cutpoint_results[[col_name]]
adds each new cut point to the list entitled "cutpoint_results", while keeping the original column names from the Data dataframe. To output a file that can be opened in Excel, you can useas.data.frame()
, followed bywrite.csv()
. I always like to setrow.names
to "F" for the latter.