-1
D = c("d", "d", "S", "d")
A = c("d", "a", "a", "x")
X = c("v", "x", "x", "t")
R = c("t", "r", "r", "r")
dat = data.frame(D, A, X, R)

D A X R MajoritySum
d d v t  1
d a x r  4
s a x r  3
d x t r  2

I am currently trying to add the MajoritySum column pictured above which counts the number of times a row has a value that is in the majority of a factor level variable.

I looped through the dataframe to grab the majority class for each column but now having difficulty from here.

majority = rep(NA, 4)
for(i in c(1:4)){
    majority[i] = 
    names(sort(table(dat[,i]),decreasing = TRUE)[1])
}

> majority
[1] "d" "a" "x" "r"
Jarrod
  • 13
  • 4
  • 2
    What is `MH.factors`? – CPak Aug 13 '18 at 17:30
  • 1
    Welcome @Jarrod - Please give an example of the output as well. Also, sometime take a look at https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Andrew Lavers Aug 13 '18 at 17:49
  • @CPak sorry that was from the actual data frame I am using, I have updated my question – Jarrod Aug 13 '18 at 18:11
  • I don't get your logic for `MajoritySum`. In the second row, none of the variables are repeated and the `largest` variable (x) is at position 3, not 4 like your example shows. – Gautam Aug 13 '18 at 18:17
  • could it be that you are looking for `rank(-table(unlist(dat))[tolower(names(dat))],ties.method = "first")`??? – Onyambu Aug 13 '18 at 18:25
  • this looks like it probably would have worked as well, thanks @Onyambu – Jarrod Aug 13 '18 at 19:22

2 Answers2

1

Here's a basic R solution:

for (i in 1:nrow(MH.factors)) {
    MH.factors$MajoritySum[i] <- sum(MH.factors[i,] == majority)
}
Edward Carney
  • 1,372
  • 9
  • 7
0

Here is a naïve solution using dplyr:

library(dplyr)

# alternatively you can use if_else(D == majority[1], 1, 0) and so on
dat %>%
  mutate(
    MajoritySum = if_else(D == "d", 1, 0) + 
      if_else(A == "a", 1, 0) + 
      if_else(X == "x", 1, 0) + 
      if_else(R == "r", 1, 0)
  )

#   D A X R MajoritySum
# 1 d d v t           1
# 2 d a x r           4
# 3 S a x r           3
# 4 d x t r           2
OzanStats
  • 2,756
  • 1
  • 13
  • 26