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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:33:44+00:00 2026-05-26T03:33:44+00:00

I seem to spend a lot of time creating a dataframe from a file,

  • 0

I seem to spend a lot of time creating a dataframe from a file, database or something, and then converting each column into the type I wanted it in (numeric, factor, character etc). Is there a way to do this in one step, possibly by giving a vector of types ?

foo<-data.frame(x=c(1:10), 
                y=c("red", "red", "red", "blue", "blue", 
                    "blue", "yellow", "yellow", "yellow", 
                    "green"),
                z=Sys.Date()+c(1:10))

foo$x<-as.character(foo$x)
foo$y<-as.character(foo$y)
foo$z<-as.numeric(foo$z)

instead of the last three commands, I’d like to do something like

foo<-convert.magic(foo, c(character, character, numeric))
  • 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-26T03:33:44+00:00Added an answer on May 26, 2026 at 3:33 am

    Edit See this related question for some simplifications and extensions on this basic idea.

    My comment to Brandon’s answer using switch:

    convert.magic <- function(obj,types){
        for (i in 1:length(obj)){
            FUN <- switch(types[i],character = as.character, 
                                       numeric = as.numeric, 
                                       factor = as.factor)
            obj[,i] <- FUN(obj[,i])
        }
        obj
    }
    
    out <- convert.magic(foo,c('character','character','numeric'))
    > str(out)
    'data.frame':   10 obs. of  3 variables:
     $ x: chr  "1" "2" "3" "4" ...
     $ y: chr  "red" "red" "red" "blue" ...
     $ z: num  15254 15255 15256 15257 15258 ...
    

    For truly large data frames you may want to use lapply instead of the for loop:

    convert.magic1 <- function(obj,types){
        out <- lapply(1:length(obj),FUN = function(i){FUN1 <- switch(types[i],character = as.character,numeric = as.numeric,factor = as.factor); FUN1(obj[,i])})
        names(out) <- colnames(obj)
        as.data.frame(out,stringsAsFactors = FALSE)
    }
    

    When doing this, be aware of some of the intricacies of coercing data in R. For example, converting from factor to numeric often involves as.numeric(as.character(...)). Also, be aware of data.frame() and as.data.frame()s default behavior of converting character to factor.

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

Sidebar

Related Questions

Voting contests seem to gain a lot of attention from people who want to
I spend a lot of time offline, and while normally I'm in the middle
Seem to run into a service endpoint not found problem when trying to get
I seem to be missing something about LINQ. To me, it looks like it's
I spent a lot of time recently reading about debugging. One of the aspects
Background Job gets mentioned a lot but all the tutorials I've seen seem to
I've got a rather hideous and large javascript file that I've inherited from a
I spent a lot of time googling, now I'll try it here. Some of
I've spent a lot of time today trying to figure out how to get
I've spent a lot of time using the Microsoft compiler cl and found that

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.