Apologies for any issues but this is my first query on this site.
I'm trying to create a new column in my dataframe which contains a new value, the name of the column that contains the lowest value across that row (each row is an individual sample). I need this new column lowest_col_name
to be created and added to the dataframe. My dataframe contains many columns and I only want it to look for this lowest value within 4 individual columns (labelled "High", "Good", "Moderate" and "Poor".
mydata[, c("lowest_col_name")] = names(mydata)[apply(mydata[,c("high","good","moderate","poorbad")], MARGIN = 1, FUN = which.min)]
Any help would be great!
I've tried many iterations of code similar to this but have had no luck.
This piece of code runs but gives me back the names of non-numeric and empty columns, columns that are not among the 4 specified. If i create a smaller dataframe with only the "High", "Good", "Moderate" and "Poor" values, and then run the code, it seems to work but i need the other information included in my output.
I don't want to split and re-join the dataframes as this would cause other unrelated issues with how I handle the data.