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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:52:44+00:00 2026-06-01T12:52:44+00:00

I understand the answer in R to repetitive things is usually apply() rather than

  • 0

I understand the answer in R to repetitive things is usually “apply()” rather than loop. Is there a better R-design pattern for a nasty bit of code I create frequently?

So, pulling tabular data from HTML, I usually need to change the data type, and end up running something like this, to convert the first column to date format (from decimal), and columns 2-4 from character strings with comma thousand separators like “2,400,000” to numeric “2400000.”

X[,1] <- decYY2YY(as.numeric(X[,1]))
X[,2] <- as.numeric(gsub(",", "", X[,2]))
X[,3] <- as.numeric(gsub(",", "", X[,3]))
X[,4] <- as.numeric(gsub(",", "", X[,4]))

I don’t like that I have X[,number] repeated on both the left and ride sides here, or that I have basically the same statement repeated for 2-4.

Is there a very R-style way of making X[,2] less repetitive but still loop-free? Something that sort of says “apply this to columns 2,3,4—a function that reassigns the current column to a modified version in place?”

I don’t want to create a whole, repeatable cleaning function, really, just a quick anonymous function that does this with less repetition.

  • 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-01T12:52:45+00:00Added an answer on June 1, 2026 at 12:52 pm

    Assuming X is a data frame, I would do:

    X[2:4] <- lapply(X[2:4], function (x) as.numeric(gsub(",", "", x)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry to repeat old questions, but I didn't quite understand the answer. The question
This might be difficult to answer. I am trying to understand some codebase and
I understand the obvious answer to my question is: it depends. With that out
I'm trying to understand whether the answer to the following question is the same
I have read and understand the answer given in this question: How do I
There was an interesting question in a practice test that I did not understand
I understand that the answer to this question may depend on registry settings and
I understand that the answer is yes - but..., and I'm just trying to
I was looking at this SO question and I couldn't understand how the answer
I've been searching and searching for an easy to understand answer, but i can't

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.