Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8550159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:53:16+00:00 2026-06-11T13:53:16+00:00

Recently I was surprised by the fact that is.logical and is.factor weren’t able to

  • 0

Recently I was surprised by the fact that is.logical and is.factor weren’t able to run out of the box with apply – at least they did not the deliver the correct result.

Here’s my little reproducible example:

# generate a dataset that contains a couple of modes
someDf <- data.frame(fac1=gl(2,3,12),
                 int=1:12,
                 char=letters[1:12],
                 logi=rep(c(T,F),6),
                 fac2=gl(3,2,12))

# hooray, this did work, got factors, int,
# characters and logical
str(someDf)

# I expected this to work, but it didn't
# everything is just FALSE
apply(someDf,2,is.logical)

I didn’t give up and found a way to sneak around this.

unlist(lapply(names(someDf),function(x) is.logical(someDf[,x])))

Though this delivers the correct result I wonder why it has to be that complicated and whether there’s a simpler to solution to it. Any ideas?

Hint: I have seen that

apply(someDf,2,class)

delivers all characters. This is unexpected, too. Maybe something with eval does the trick I couldn’t find.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:53:18+00:00Added an answer on June 11, 2026 at 1:53 pm

    Since a data.frame is a list, you should use lapply or sapply:

    sapply(someDf, is.logical)
     fac1   int  char  logi  fac2 
    FALSE FALSE FALSE  TRUE FALSE 
    

    The reason that your code doesn’t work is because apply needs a matrix as its argument, and coerces to a matrix if you provide a data frame. Since a matrix can only have elements of a single class, your values get converted to character. Try it:

    as.matrix(someDf)
          fac1 int  char logi    fac2
     [1,] "1"  " 1" "a"  " TRUE" "1" 
     [2,] "1"  " 2" "b"  "FALSE" "1" 
     [3,] "1"  " 3" "c"  " TRUE" "2" 
     [4,] "2"  " 4" "d"  "FALSE" "2" 
     [5,] "2"  " 5" "e"  " TRUE" "3" 
     [6,] "2"  " 6" "f"  "FALSE" "3" 
     [7,] "1"  " 7" "g"  " TRUE" "1" 
     [8,] "1"  " 8" "h"  "FALSE" "1" 
     [9,] "1"  " 9" "i"  " TRUE" "2" 
    [10,] "2"  "10" "j"  "FALSE" "2" 
    [11,] "2"  "11" "k"  " TRUE" "3" 
    [12,] "2"  "12" "l"  "FALSE" "3" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was recently surprised to know that this code compiles (at least on gcc
I recently was surprised to find that this code would run on Python 2.7
Recently I have started learning rails and was a little surprised that the default
I was recently surprised to note that compiling with /GS (Enable buffer security check)
I was surprised recently to find that it's possible to have a return statement
I recently wrote this and was surprised that it compiles: public class MyGeneric<U, V>
I've recently started learning Scala and was disappointed (but not surprised) that their generics
Recently I've discovered M-x ielm (the Emacs self-REPL) and was pleasantly surprised that there's
I was surprised recently to discover that the compiler is apparently not strict about
Recently I was able to create a tablet software for my Cerebral Palsy girl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.