[Solved] R Programming- Need to calculate upto three most common values of an attribute in sorted order


So, as you can see, not having a reproducible example can call the hounds on you. Nevertheless, I feel that your problem was well-described, if not well researched, and thus deserves an answer (if not too time consuming). In this case, there are some basic data manipulation functions that you will have to familiarize yourself with. The catfunction will allow you to return a text report with the results. Here is an example:

# make data
df <- data.frame(Yes_No = sample(c("Yes", "No"), 10, replace=TRUE))
df

# convert "Yes_No" to factor
df$Yes_No <- factor(df$Yes_No, levels=c("Yes", "No", "Maybe"))

# summary
summary(df)

# or aggregate
res <- aggregate(df, by=list(df$Yes_No), FUN=length)
res
cat(" MCV\n", "--------------\n", paste(res[,1], " (", res[,2], ")", sep="", collapse=" "))

# MCV
# --------------
# Yes (7) No (3)

##### ALT w/ "Maybe" ###
df <- data.frame(Yes_No = sample(c("Yes", "No", "Maybe"), 20, replace=TRUE))
df$Yes_No <- factor(df$Yes_No, levels=c("Yes", "No", "Maybe"))
res <- aggregate(df, by=list(df$Yes_No), FUN=length)
res
cat(" MCV\n", "--------------\n", paste(res[,1], " (", res[,2], ")", sep="", collapse=" "))
# MCV
# --------------
# Yes (9) No (6) Maybe (5)

1

solved R Programming- Need to calculate upto three most common values of an attribute in sorted order