Basil
Basil

Reputation: 1004

Count number of entries in each column with result in dataframe

I have a dataframe with many columns. I want to count the number of times something is entered into each column.

#Example data
Gender<-c("","Male","Male","","Female","Female")
location<-c("UK","France","USA","","","")
dataset<-data.frame(Gender,location, stringsAsFactors = FALSE)

There are 4 entries in the gender column and 3 entries in the location column.

I want the results to be in a dataframe such as:

result<-data.frame(Results=c("Gender","location"), Totals=c(4,3))

Can anyone suggest an approach to do this?

Upvotes: 1

Views: 1030

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 102607

A base R option using stack + colSums

setNames(
  rev(stack(colSums(dataset != ""))),
  c("Results", "Total")
)

gives

   Results Total
1   Gender     4
2 location     3

Upvotes: 1

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21442

You can use the namesof datasetas one column for resultand calculate the Totals by counting how often grep matches anything that is a character (as opposed to nothing in an empty cell):

result <- data.frame(
  Results = names(dataset),
  Totals = sapply(dataset, function(x) length(grep(".", x)))
)
rownames(result) <- NULL

Result:

result
   Results Totals
1   Gender      4
2 location      3

Upvotes: 3

user15060754
user15060754

Reputation: 29

This should work for you:

ngen <- sum(dataset$Gender != "") #sum number entries in column that are not empty

nloc <- sum(dataset$location != "") #sam thing

Totals <- c(ngen,nloc)


result<-data.frame(Results=c("Gender","location"), Totals)

You can simplify some of the steps if you want, but that would be the detailed way.

Upvotes: 1

Related Questions