How to find out how many times a certain level is repeated in a specific column (factor) of a data.frame?

Asked

Viewed 51 times

0

I am working with 4 data frames (W, X, Y and Z) that have the same number of variables. Factor (column/variable) 11 was classified into 3 different levels (1, 2, 3). I need to find out how many times each of these levels repeats in this Column 11 in each of the data.frames. I believe that the most ideal package to work with this type of data manipulation is dplyr, but I’m not able to assemble the code.

inserir a descrição da imagem aqui

  • 4

    I think the function table() can help you: table(W$Coluna11)

  • If you only need this information I recommend using the same table. If you want the answer as a data.frame to use later (for a chart, Join or for a better print) then the dplyr is a good option too. W %>% count(Coluna11) would be your solution.

2 answers

2

Using only R base: place the data frames. in a list and apply table to her:

# Dados de exemplo
set.seed(87365)
W <- data.frame(Coluna1 = LETTERS[1:20],
                Coluna2 = as.factor(sample(1:3, 20, TRUE)))
W -> X -> Y -> Z

dfs <- c("W", "X", "Y", "Z")
# se eles seguem algum padrão, pode usar dfs <- objects(pattern = "padrao")

df.list <- mget(dfs)

> sapply(df.list, function(x) table(x$Coluna2))
  W X Y Z
1 8 8 8 8
2 5 5 5 5
3 7 7 7 7

If all data.frames have the same variables, you can join them with an identification column and use it for grouping:

dados <- Reduce(rbind, Map(cbind, df.list, df = names(df.list)))
# ou
dados <- dplyr::bind_rows(df.list, .id = "df")
# ou
dados <- data.table::rbindlist(df.list, idcol = "df")

> table(dados$Coluna2, dados$df)
  
    W X Y Z
  1 8 8 8 8
  2 5 5 5 5
  3 7 7 7 7

0


I believe Summary function works for what you want.

summary(df$variavel)

As quoted in the comment by Willian the function table() also solves your problem.

table(df$variavel)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.