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 658369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:55:33+00:00 2026-05-13T22:55:33+00:00

I want to convert variables into factors using apply() : a <- data.frame(x1 =

  • 0

I want to convert variables into factors using apply():

a <- data.frame(x1 = rnorm(100),
                x2 = sample(c("a","b"), 100, replace = T),
                x3 = factor(c(rep("a",50) , rep("b",50))))

a2 <- apply(a, 2,as.factor)
apply(a2, 2,class)

results in:

         x1          x2          x3 
"character" "character" "character" 

I don’t understand why this results in character vectors instead of factor vectors.

  • 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-05-13T22:55:34+00:00Added an answer on May 13, 2026 at 10:55 pm

    apply converts your data.frame to a character matrix. Use lapply:

    lapply(a, class)
    # $x1
    # [1] "numeric"
    # $x2
    # [1] "factor"
    # $x3
    # [1] "factor"
    

    In second command apply converts result to character matrix, using lapply:

    a2 <- lapply(a, as.factor)
    lapply(a2, class)
    # $x1
    # [1] "factor"
    # $x2
    # [1] "factor"
    # $x3
    # [1] "factor"
    

    But for simple lookout you could use str:

    str(a)
    # 'data.frame':   100 obs. of  3 variables:
    #  $ x1: num  -1.79 -1.091 1.307 1.142 -0.972 ...
    #  $ x2: Factor w/ 2 levels "a","b": 2 1 1 1 2 1 1 1 1 2 ...
    #  $ x3: Factor w/ 2 levels "a","b": 1 1 1 1 1 1 1 1 1 1 ...
    

    Additional explanation according to comments:

    Why does the lapply work while apply doesn’t?

    The first thing that apply does is to convert an argument to a matrix. So apply(a) is equivalent to apply(as.matrix(a)). As you can see str(as.matrix(a)) gives you:

    chr [1:100, 1:3] " 0.075124364" "-1.608618269" "-1.487629526" ...
    - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : chr [1:3] "x1" "x2" "x3"
    

    There are no more factors, so class return "character" for all columns.
    lapply works on columns so gives you what you want (it does something like class(a$column_name) for each column).

    You can see in help to apply why apply and as.factor doesn’t work :

    In all cases the result is coerced by
    as.vector to one of the basic vector
    types before the dimensions are set,
    so that (for example) factor results
    will be coerced to a character array.

    Why sapply and as.factor doesn’t work you can see in help to sapply:

    Value (…) An atomic vector or matrix
    or list of the same length as X (…)
    If simplification occurs, the output
    type is determined from the highest
    type of the return values in the
    hierarchy NULL < raw < logical <
    integer < real < complex < character <
    list < expression, after coercion of
    pairlists to lists.

    You never get matrix of factors or data.frame.

    How to convert output to data.frame?

    Simple, use as.data.frame as you wrote in comment:

    a2 <- as.data.frame(lapply(a, as.factor))
    str(a2)
    'data.frame':   100 obs. of  3 variables:
     $ x1: Factor w/ 100 levels "-2.49629293159922",..: 60 6 7 63 45 93 56 98 40 61 ...
     $ x2: Factor w/ 2 levels "a","b": 1 1 2 2 2 2 2 1 2 2 ...
     $ x3: Factor w/ 2 levels "a","b": 1 1 1 1 1 1 1 1 1 1 ...
    

    But if you want to replace selected character columns with factor there is a trick:

    a3 <- data.frame(x1=letters, x2=LETTERS, x3=LETTERS, stringsAsFactors=FALSE)
    str(a3)
    'data.frame':   26 obs. of  3 variables:
     $ x1: chr  "a" "b" "c" "d" ...
     $ x2: chr  "A" "B" "C" "D" ...
     $ x3: chr  "A" "B" "C" "D" ...
    
    columns_to_change <- c("x1","x2")
    a3[, columns_to_change] <- lapply(a3[, columns_to_change], as.factor)
    str(a3)
    'data.frame':   26 obs. of  3 variables:
     $ x1: Factor w/ 26 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ...
     $ x2: Factor w/ 26 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10 ...
     $ x3: chr  "A" "B" "C" "D" ...
    

    You could use it to replace all columns using:

    a3 <- data.frame(x1=letters, x2=LETTERS, x3=LETTERS, stringsAsFactors=FALSE)
    a3[, ] <- lapply(a3, as.factor)
    str(a3)
    'data.frame':   26 obs. of  3 variables:
     $ x1: Factor w/ 26 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ...
     $ x2: Factor w/ 26 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10 ...
     $ x3: Factor w/ 26 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10 ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 405k
  • Answers 405k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, it would be a good practice - trouble is:… May 15, 2026 at 5:41 am
  • Editorial Team
    Editorial Team added an answer It doesn't look like a side-by-side error. The exception code… May 15, 2026 at 5:41 am
  • Editorial Team
    Editorial Team added an answer You should use open with the w+ mode: file =… May 15, 2026 at 5:41 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.