As input we have this data structure:
dframe <- data.frame(id = c(1,2,3), col1 = c(4.2, 1.2,0), su = c(1.2,2.4,0))
The code to create new column
com_num <- with(dframe, pmax(col1, su))
i <- which(dframe[-1] == com_num, arr.ind = TRUE)
dframe$com <- names(dframe[-1])[i[, 2]]
dframe$com_num <- com_num
But this error exist
Error in `$<-.data.frame`(`*tmp*`, com, value = c("col1", "col1", "su", :
replacement has 4 rows, data has 3
I think this is because of zero equal to zero. If this is the situation how can we add when it detect zero equals to zero add a text "zerohere"?
Expected output:
> dframe <- data.frame(id = c(1,2,3), col1 = c(4.2, 1.2,0), su = c(1.2,2.4,0), com = c("col1","su","zerohere"), com_num = c(4.2,2.4,0))
> dframe
id col1 su com com_num
1 1 4.2 1.2 col1 4.2
2 2 1.2 2.4 su 2.4
3 3 0.0 0.0 zerohere 0.0