Measure Theory Penguin
Measure Theory Penguin

Reputation: 135

R: How to do this without a for-loop?

The following code in R uses a for-loop. What is a way I could solve the same problem without a for-loop (maybe by vectorizing it)?

I am looking at an unfamiliar dataset with many columns (243), and am trying to figure out which columns hold unstructured text. As a first check, I was going to flag columns that are 1) of class 'character' and 2) have at least ten unique values.

openEnded <- rep(x = NA, times = ncol(scaryData))
for(i in 1:ncol(scaryData)) {
  openEnded[i] <- is.character(scaryData[[i]]) & length(unique(scaryData[[i]])) >= 10
  }

Upvotes: 1

Views: 90

Answers (1)

Vincent Bonhomme
Vincent Bonhomme

Reputation: 7443

This would probably do the job:

openEnded <- apply(scaryData, 2, function(x) is.character(x) & length(unique(x))>=10)

From the loop, you simply iterate over columns (that's the apply(scaryData, 2) part) an anonymous function that combines your two conditions (function(x) cond1 & cond2).

I guess your data is a data.frame so sapply(scaryData, 2, function(x) ...) would also work.

A nice post about the *apply family can be found there.

Upvotes: 1

Related Questions