В вашем примере диапазон = 7:10. Перейдите на любые нужные столбцы.
#your data frame
X1 <- c(5,1,2,2,5)
X2 <- c(2,4,3,5,2)
X3 <- c(6,6,2,3,3)
X4 <- c(5,1,0,3,0)
X5 <- c(0,3,3,1,1)
X6 <- c(6,4,3,3,5)
X7 <- c(2,3,5,3,1)
X8 <- c(5,2,0,5,2)
X9 <- c(2,2,1,3,1)
X10 <- c(5,3,6,0,0)
df <- data.frame(X1=X1, X2=X2, X3=X3, X4=X4, X5=X5, X6=X6, X7=X7, X8=X8, X9=X9, X10=X10)
df
#change the range for desired columns
range <- 1:length(colnames(df))
#create an empty data frame
x <- data.frame(Var1=0, Freq=0)
#for-loop to count frequencies of each desired column and merge to one data frame
for (i in range) {
y <- as.data.frame(table(df[,i]))
x <- merge(x=x, y=y, by="Var1", all=TRUE)
}
#make NA values equivalent to 0
x[is.na(x)] <- 0
#new data frame to extract elements and sum frequencies of each element
z <- data.frame(Element=x[ ,1], Frequency=rowSums(x[ ,2:length(colnames(x))]))
#order by descending frequency
z <- z[order(-z$Frequency),]
#rank by descending frequency
z$Rank <- c(length(z$Element):1)
z