Reputation: 4771
I have this chunk of code in an R script, and what it does is that from a data frame (that has say 4 columns) finds and counts for each row the column with the maximal value. But when in a row there are ties (say two or maybe three max values), the code below just considers the first one (does not count the other ones). Is there in R any way to count the columns with max value even when there are multiple max values (ties?) in rows.
m_0 <- read.csv(file="Tests/myResults", head=FALSE, sep=",")
varb_m0 <- c(m0$ V4)
#create dataframe
myDataFrame <- data.frame(mode_0=varb_m0)
#find max
result <- apply(myDataFrame,1,which.max)
#factor it
result <- factor(result)
#print result
names(myDataFrame)[result]
summary(result)
Upvotes: 1
Views: 3864
Reputation: 83
A simple way to do it would be to make a function first:
all_max <- function(x) {which(x == max(x))} #Index of each value that is maximum.
And then just apply that to your data frame:
result <- apply(myDataFrame,1,all_max)
I think the rest should be the same, though you didn't provide any data to test it on.
Upvotes: 2