-2

I have a dataframe like so:

   X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1   5  2  6  5  0  6  2  5  2   5
2   1  4  6  1  3  4  3  2  2   3
3   2  3  2  0  3  3  5  0  1   6
4   2  5  3  3  1  3  3  5  3   0
5   5  2  3  0  1  5  1  2  1   0

Is there a way to find the frequency of elements in, let's say columns X7 through X10 and represent this numerically? in a way similar to rank()? So, 2 would be 1, 0 and 5 would be 2, and etc. ?

Bob
  • 35
  • 4
  • 1
    I don't know if it's just me but I can't quite get the logic. Why should 2 be 1,0? – NelsonGon Jun 03 '19 at 05:18
  • 3
    I guess you need `rle` or could be `table(unlist(df1[7:10]))` – akrun Jun 03 '19 at 05:21
  • 2
    Can you also show what the final output would look like? Can you make your input data copy/pastable, e.g. using `dput`? See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to prepare code for a question. – Roman Luštrik Jun 03 '19 at 06:10

1 Answers1

0

In your example, range = 7:10. Change to whatever desired columns.

#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
wisamb
  • 470
  • 3
  • 11
  • As mentioned in a comment above: `table(unlist(df))` would have gotten you half way to your `z` without the need for merge within loop. From there, you can add orders as you did, or with rank: `rank(-table(unlist(df)))` – Jav Jun 03 '19 at 09:51