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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:06:46+00:00 2026-06-01T22:06:46+00:00

I am looking to use a function to speed up a data cleaning process.

  • 0

I am looking to use a function to speed up a data cleaning process. In the example shown I am looking to remove values reported in the am and pm columns if the “.no” column for that day has a value of 1.

df1 = data.frame (identifier = c(1:4),
mon.no = c(1,NA,NA,NA),mon.am = c(2,1,NA,3),mon.pm = c(3,4,NA,5),
tues.no = c(NA,NA,1,NA),tues.am = c(2,3,1,4),tues.pm = c(3,3,2,3))

I envisage using a function uses the day to clean the data:

clean1 = function (day) {
df1$day.am[df1$day.no==1] = NA
df1$day.pm[df1$day.no==1] = NA
return (df1)}
df2 = clean1(mon)

However this returns the following error.

Error in `$<-.data.frame`(`*tmp*`, "day.am", value = logical(0)) : 
replacement has 0 rows, data has 4

I assume that this is because the function expects a full column name and cannot fill in the gaps around a text input? Is it possible to use a function in that way?

Having read these notes I think that it would be better practice to have my data in a tidy format and am working on a solution which involves reorganising my data. However it would also be handy to be able to do this while the data is in it’s original format.

Thanks.

  • 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-01T22:06:49+00:00Added an answer on June 1, 2026 at 10:06 pm

    You’re really close. @Tyler Rinker in comments has explained why it doesn’t work. Here’s a fix:

    clean1 = function (day) {
      day.am = paste(day, "am", sep=".") # make a string from the variable day and the suffixes
      day.pm = paste(day, "pm", sep=".")
      day.no = paste(day, "no", sep=".")
      df1[day.am][df1[day.no]==1] = NA 
      df1[day.pm][df1[day.no]==1] = NA
      return (df1)}
    
    df2 = clean1("mon")   # "mon" should be a string
    

    Somebody else might offer more efficient ways of doing this. Note that you’re only ever working from your original df1 here. If you now run

    df3 = clean1("tues")
    

    you won’t get a dataframe with both days cleaned. You could fix this by supplying the dataframe to be acted on to the function too:

    clean2 = function(df, day){...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a hash function that I can use to give uniform unique
I'm looking for the equivalent to the PHP function mysql_real_escape_string() to use in C#.NET.
How can use the slideDown() function with easing? Maybe extend it somehow? I'm looking
I'm looking for a way to use a function as an argument to another
I have some vectors of experimental data that I need to massage, for example:
I'm looking to speed up the following algorithm. I give the function an xts
I am looking to use an IOC container in my current project which is
I am looking to use Adobe Air to visualize information coming from the Serial
I am looking to use/buy a OCR solution for my next iPhone app. Searching
I'm looking to use NVelocity in my ASP.NET MVC application, not as a view

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.