LA_
LA_

Reputation: 20419

How to output all levels, which length is not equal to 3?

I would like to display all levels, which length (as character) is not equal to 3 symbols.

The following code returns length of dt$col for each row:

with(dt, nchar(as.character(dt$col)))

But if I pass levels(dt$col) as the first argument, it fails with the following error:

Error in eval(substitute(expr), data, enclos = parent.frame()) :
invalid 'envir' argument of type 'character'

How to count length for each level? How to display levels with wrong length only?

Upvotes: 0

Views: 643

Answers (3)

lebatsnok
lebatsnok

Reputation: 6469

Some ways to do it in one line:

(l <- levels(f$myFactor))[nchar(l)!=3]
(function(x) x[nchar(x)!=3])(levels(f$myFactor))
levels(f$myFactor) %>% {.[nchar(.)!=3]}  # uses library(magrittr)

Upvotes: 0

BenBarnes
BenBarnes

Reputation: 19454

First, create a new object with your factor levels (using the data from DatamineR's answer, except NOT using factor as a column name):

f <- data.frame(myFactor = factor(c("ABCD", "ABC", "A", "ABCDE", "ABC")))
myLevs <- levels(f$myFactor)

Then subset the levels that are not three characters long:

myLevs[nchar(myLevs) != 3]
## [1] "A"     "ABCD"  "ABCDE"

Upvotes: 1

DatamineR
DatamineR

Reputation: 9618

If you are using with you should not be using dt$, you can refer to the column names directly.

# Some factor data
f <- data.frame(factor = factor(c("ABCD", "ABC", "A", "ABCDE", "ABC")))

# Cont the lengths of the factors
with(f, nchar(as.character(factor)))
[1] 4 3 1 5 3

# Display the levels whose length is not equal to 3
f$factor[with(f, which(nchar(as.character(factor)) !=3)), drop = TRUE]
[1] ABCD  A     ABCDE
Levels: A ABCD ABCDE

Upvotes: 1

Related Questions