0

In my dataset I have multiple columns which are character vectors (columns) with the same set of variables.

Lets say they are "Very Good" "Good" "Neutral" "Bad" "Very Bad".

Now my goal is to get a data.frame output, which displays for each column the number or count (n) for each of these variables.

I have tried:

apply(df[c("Col1", "Col2", "Col3")],2, table)

which gives me a good output, as integer vectors.

The problem is, that if the length of of these vectors differs, I get a list of integers, which i can not easily put into a data.frame to then work further with.

Are there any options to work around this?

  • Please add data using `dput` or something that we can copy and use. Also show expected output for the data shared. Read about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Dec 09 '20 at 12:18

1 Answers1

1

One approach is to add NA values to equalize the length of each list element, like lukeA's answer here, and then convert the list to a data.frame. In your case it would look like this:

# generate sample data
levels <- c("Very Good", "Good" ,"Neutral", "Bad", "Very Bad")
set.seed(3)
df <- data.frame(Col1 = sample(levels[1:3], 10, replace = TRUE),
            Col2 = sample(levels, 10, replace = TRUE),
                 Col3 = sample(levels, 10, replace = TRUE))
# get counts
list <- apply(df,2, table)

# lengthen shorter integer vectors
list <- lapply(tbl, `length<-`, max(lengths(list)))

# convert to data.frame
tbl_df <- as.data.frame(do.call(cbind, list))

tbl_df
xilliam
  • 2,074
  • 2
  • 15
  • 27