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

  • Home
  • SEARCH
  • 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 8682605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:49:21+00:00 2026-06-12T21:49:21+00:00

I have a dataframe with 100 variables, of which I would like a subset,

  • 0

I have a dataframe with 100 variables, of which I would like a subset, say dataframename[,30:50] to be converted to their original numeric values (1,2,3,4,5).

I know that I should use as.numeric(levels(f))[f] when I convert a factor, but I can only make this work when I convert the factors one at a time. I would like to convert them all at once.

This wont work:

as.numeric(levels(dataframename[,30:50]))[dataframename[,30:50]]  

neither will this:

sapply(dataframename[,30:50],as.numeric(levels(dataframename[,30:50]))    
[dataframename[,30:50]]  

Any ideas or stuff I should read?

  • 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-12T21:49:22+00:00Added an answer on June 12, 2026 at 9:49 pm

    This is a smaller example but the idea should hold. You can use lapply to apply your conversion to each column of your data frame and then just replace those columns directly.

    # make example data
    dat <- as.data.frame(lapply(as.data.frame(matrix(seq(2*3), ncol = 3)), factor))
    
    factorconvert <- function(f){as.numeric(levels(f))[f]}
    dat[, 2:3] <- lapply(dat[, 2:3], factorconvert)
    dat
    #  V1 V2 V3
    #1  1  3  5
    #2  2  4  6
    #str(dat)
    #'data.frame':  2 obs. of  3 variables:
    # $ V1: Factor w/ 2 levels "1","2": 1 2
    # $ V2: num  3 4
    # $ V3: num  5 6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe with >100 columns, and I would to find the unique
Let's say I have a data.frame like: a <- c(1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10,1:10) df <- data.frame(a,rnorm(100)) And
A week ago I would have done this manually: subset dataframe by group to
I have the following dataframe: sp <- combn(c(sp1,sp2,sp3,sp4),2) d <- data.frame(t(sp),freq=sample(0:100,6)) and two factors
I have a dataframe x with this values: x1 x2 x3 1 NA 4
I have a dataframe with some numeric columns. Some row has a 0 value
I have a dataframe called split2_data (actually a drop-leveled subset of a bigger data
I have a dataset with 224900 observations and 10 variables which are the result
I have the following question: I have data frame which looks like this. I
I have a dataframe in R like this: dat = data.frame(Sample = c(1,1,2,2,3), Start

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.