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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:51:39+00:00 2026-05-29T11:51:39+00:00

You know how you can supply a vector of names to a data frame

  • 0

You know how you can supply a vector of names to a data frame to change the col or row names of a dataframe. Is there a similar method to supply a vector of names that alters the class of each column in a dataframe? You can do this when you read in a dataframe with read.table using colClasses. What about if the dataframe is created inside R?

DF <- as.data.frame(matrix(rnorm(25), 5, 5))
str(DF)  #all numeric modes

names(DF) <- c("A", "A2", "B", "B2", "Z") #I want something like this for classes
some_classes_function_like_names(DF) <- c(rep("character", 3), rep("factor", 2))

#I can do it like this but this seems inefficient 
DF[, 1:3] <- lapply(DF[, 1:3], as.character)
DF[, 4:5] <- lapply(DF[, 4:5], as.factor)

str(DF)

EDIT: I changed sapply above to lapply as sapply doesn’t make sense.

EDIT 2: If there’s a way to write a user defined function that would suffice as well

  • 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-29T11:51:41+00:00Added an answer on May 29, 2026 at 11:51 am

    It seems class(x) <- "factor" doesn’t work and neither does as(x, "factor"), so I don’t know of a direct way of doing what you want.

    …But a slightly more explicit way is:

    # Coerces data.frame columns to the specified classes
    colClasses <- function(d, colClasses) {
        colClasses <- rep(colClasses, len=length(d))
        d[] <- lapply(seq_along(d), function(i) switch(colClasses[i], 
            numeric=as.numeric(d[[i]]), 
            character=as.character(d[[i]]), 
            Date=as.Date(d[[i]], origin='1970-01-01'), 
            POSIXct=as.POSIXct(d[[i]], origin='1970-01-01'), 
            factor=as.factor(d[[i]]),
            as(d[[i]], colClasses[i]) ))
        d
    }
    
    # Example usage
    DF <- as.data.frame(matrix(rnorm(25), 5, 5))
    DF2 <- colClasses(DF, c(rep("character", 3), rep("factor", 2)))
    str(DF2)
    
    DF3 <- colClasses(DF, 'Date')
    str(DF3)
    

    A couple of things: you can add more cases as needed. And the first line of the function allows you to call with a single class name. The last “default” case of the switch calls the as function and you mileage might vary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that I know can be done using a subselect, but
You can know if the event stack is empty calling the gtk.events_pending() method, but
Does anybody know of a library or piece of software out there that will
There are a huge feature collection in C++ programming language that supply a strict
I want to supply a default value which can be overridden. I know I
I know that in C# (and VB.Net) I can create a delegate, point this
This is something I know can be done somehow , because I've done it
I need to know can we add image in TextArea through StyleableTextField htmlText because
I know you can ALTER the column order in MySQL with FIRST and AFTER,
I know we can explicitly call the constructor of a class in C++ using

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.