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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:28:33+00:00 2026-05-16T17:28:33+00:00

I´m trying to clean the factor variables in a dataframe from trailing spaces. However

  • 0

I´m trying to clean the factor variables in a dataframe from trailing spaces. However the levels assignment doesnt work inside my lapply function.

rm.space<-function(x){
    a<-gsub(" ","",x)
    return(a)}


lapply(names(barn),function(x){
    levels(barn[,x])<-rm.space(levels(barn[,x]))
    })

Any ideas how I can assign levels inside a lapply function?

//M

  • 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-16T17:28:33+00:00Added an answer on May 16, 2026 at 5:28 pm

    From your code I read that the lapply is used to loop over different variables, not over the levels of the factor. So then you do need some kind of looping structure, but lapply is a bad choice:

    • you loop over a vector -names(barn)- so it’s better to use sapply
    • the apply family will return the result from each loop, something you don’t want. So you’re using memory without purpose.

    Anyway, in case you need to assign something to a variable in your global environment within a lapply, you need the <<- operator. Say you need to have a number of variables you selected where the spaces have to be removed:

    f <- paste("",letters[1:5])
    
    Df <- data.frame(
        X1 = sample(f,10,r=T),
        X2 = sample(f,10,r=T),
        X3 = sample(f,10,r=T)
        )
    
    # Bad example :   
    lapply(c("X1","X3"),function(x){
        levels(Df[,x])<<-gsub(" +","",levels(Df[,x]))
        })
    

    gives

    > str(Df)
    'data.frame':   10 obs. of  3 variables:
     $ X1: Factor w/ 3 levels "a","b","c": 2 3 1 1 1 2 3 2 2 2
     $ X2: Factor w/ 5 levels " a"," b"," c",..: 4 5 4 2 5 5 1 2 5 3
     $ X3: Factor w/ 5 levels "a","b","c","d",..: 2 3 4 1 4 1 3 3 5 4
    

    Better is to use a for loop :

    for( i in c("X1","X3")){
        levels(Df[,i])<-gsub(" +","",levels(Df[,i]))
    }
    

    Does what you need without the hassle of the <<- operator and without holding memory unnecessarily.

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

Sidebar

Related Questions

I have output from grep I'm trying to clean up that looks like: <words>Http://www.path.com/words</words>
I'm trying to clean up the architecture of my jQuery Mobile application by using
I am trying to clean up a ByteBuffer to be all zero bytes (all
I'm trying to clean-up after an exception, and I'm not sure how to handle
I am trying to clean up some of the JavaScript throughout my views and
I'm trying to clean up form input using the following Perl transliteration: sub ValidateInput
I'm trying to clean duplicate code. The only difference are calls like MyType x
I'm trying to clean up some warnings in some old Java code (in Eclipse),
I am trying to clean up some data that has been incorrectly entered. The
This simple problem is kiling me. I posted something earlier about trying to clean

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.