# count values in rows and define a new colunmn

Hi,
I'm struggling at comparing if there is more A or B in each row, and I want to have a new column containing either A or B whichever is more in each row. (if A is more in one row, show 'A')

Thank you very much for your helping！

Here is one way to do what you want.

``````DF <- data.frame(V1 = sample(c("A", "B"), 10, replace = TRUE),
V2 = sample(c("A", "B"), 10, replace = TRUE),
V3 = sample(c("A", "B"), 10, replace = TRUE),
V4 = sample(c("A", "B"), 10, replace = TRUE),
V5 = sample(c("A", "B"), 10, replace = TRUE),
V6 = sample(c("A", "B"), 10, replace = TRUE),
V7 = sample(c("A", "B"), 10, replace = TRUE),
V8 = sample(c("A", "B"), 10, replace = TRUE),
V9 = sample(c("A", "B"), 10, replace = TRUE), stringsAsFactors = FALSE)
DF
#>    V1 V2 V3 V4 V5 V6 V7 V8 V9
#> 1   B  A  A  B  A  A  A  B  B
#> 2   A  A  B  A  A  A  A  A  A
#> 3   A  B  A  A  B  B  B  A  B
#> 4   A  B  A  A  A  A  A  A  A
#> 5   A  A  B  B  A  A  A  B  A
#> 6   A  B  A  B  A  B  A  B  A
#> 7   B  B  A  B  A  A  B  B  B
#> 8   A  B  B  A  B  B  A  B  A
#> 9   A  A  B  A  A  B  B  B  B
#> 10  B  B  B  B  A  A  A  B  B
COUNT_A <- function(row) {
if(sum(row == "A") >= sum(row == "B")) {
tmp <- "A"
} else {
tmp <- "B"
}
tmp
}
NewCol <- apply(DF, MARGIN = 1, FUN = COUNT_A)
DF\$NewColumn <- NewCol
DF
#>    V1 V2 V3 V4 V5 V6 V7 V8 V9 NewColumn
#> 1   B  A  A  B  A  A  A  B  B         A
#> 2   A  A  B  A  A  A  A  A  A         A
#> 3   A  B  A  A  B  B  B  A  B         B
#> 4   A  B  A  A  A  A  A  A  A         A
#> 5   A  A  B  B  A  A  A  B  A         A
#> 6   A  B  A  B  A  B  A  B  A         A
#> 7   B  B  A  B  A  A  B  B  B         B
#> 8   A  B  B  A  B  B  A  B  A         B
#> 9   A  A  B  A  A  B  B  B  B         B
#> 10  B  B  B  B  A  A  A  B  B         B
``````

Created on 2020-05-03 by the reprex package (v0.3.0)

One more way to do it:

``````# for reproducibility
set.seed(seed = 64386)

# random generated data
sample_df <- as.data.frame(x = matrix(data = sample(x = c("A", "B"),
size = 100,
replace = TRUE),
nrow = 10))

# main code
sample_df[["new_column"]] <- ifelse(test = rowMeans(x = sample_df == "A") >= 0.5,
yes = "A",
no = "B")

# checking results
sample_df
#>    V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 new_column
#> 1   A  A  B  B  B  A  B  A  B   B          B
#> 2   B  B  B  A  A  A  A  A  A   A          A
#> 3   A  A  B  B  B  A  B  B  B   A          B
#> 4   B  B  B  A  B  A  A  B  B   A          B
#> 5   A  B  A  B  B  A  B  A  A   B          A
#> 6   A  B  A  A  B  B  A  A  B   B          A
#> 7   A  A  A  A  A  B  B  A  B   A          A
#> 8   B  B  A  B  B  B  A  A  B   B          B
#> 9   B  B  B  A  B  B  B  A  A   B          B
#> 10  A  A  B  B  A  B  B  B  B   B          B
``````

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.