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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:11:11+00:00 2026-05-26T02:11:11+00:00

I have a large database made up of mixed data types (numeric, character, factor,

  • 0

I have a large database made up of mixed data types (numeric,
character, factor, ordinal factor) with missing values, and I am
trying to create a for loop to substitute the missing values
using either the mean of the respective column if numerical or the mode if character/factor.

This is what I have until now:

#fake array:
age<- c(5,8,10,12,NA)
a <- factor(c("aa", "bb", NA, "cc", "cc"))
b <- c("banana", "apple", "pear", "grape", NA)
df_test <- data.frame(age=age, a=a, b=b)
df_test$b <- as.character(df_test$b)

for (var in 1:ncol(df_test)) {
    if (class(df_test[,var])=="numeric") {
        df_test[is.na(df_test[,var]) <- mean(df_test[,var], na.rm = TRUE)
} else if (class(df_test[,var]=="character") {
        Mode(df_test$var[is.na(df_test$var)], na.rm = TRUE)
} 
}

Where ‘Mode’ is the function:

Mode <- function (x, na.rm) {
    xtab <- table(x)
    xmode <- names(which(xtab == max(xtab)))
    if (length(xmode) > 1)
        xmode <- ">1 mode"
    return(xmode)
}

It seems as it is just ignoring the statements though, without giving
any error…
I have also tried to work the first part out with indexes:

## create an index of missing values
index <- which(is.na(df_test)[,1], arr.ind = TRUE)
## calculate the row means and "duplicate" them to assign to appropriate cells
df_test[index] <- colMeans(df_test, na.rm = TRUE) [index["column",]]

But I get this error: “Error in colMeans(df_test, na.rm = TRUE) : ‘x’ must be numeric”

Does anybody have any idea how to solve this?

Thank you very much for all the great help!
-f

  • 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-26T02:11:11+00:00Added an answer on May 26, 2026 at 2:11 am

    If you simply remove the obvious bugs then it works as intended:

    Mode <- function (x, na.rm) {
        xtab <- table(x)
        xmode <- names(which(xtab == max(xtab)))
        if (length(xmode) > 1) xmode <- ">1 mode"
        return(xmode)
    }
    
    # fake array:
    age <- c(5, 8, 10, 12, NA)
    a <- factor(c("aa", "bb", NA, "cc", "cc"))
    b <- c("banana", "apple", "pear", "grape", NA)
    df_test <- data.frame(age=age, a=a, b=b)
    df_test$b <- as.character(df_test$b)
    
    print(df_test)
    
    #   age    a      b
    # 1   5   aa banana
    # 2   8   bb  apple
    # 3  10 <NA>   pear
    # 4  12   cc  grape
    # 5  NA   cc   <NA>
    
    for (var in 1:ncol(df_test)) {
        if (class(df_test[,var])=="numeric") {
            df_test[is.na(df_test[,var]),var] <- mean(df_test[,var], na.rm = TRUE)
        } else if (class(df_test[,var]) %in% c("character", "factor")) {
            df_test[is.na(df_test[,var]),var] <- Mode(df_test[,var], na.rm = TRUE)
        }
    }
    
    print(df_test)
    
    #     age  a       b
    # 1  5.00 aa  banana
    # 2  8.00 bb   apple
    # 3 10.00 cc    pear
    # 4 12.00 cc   grape
    # 5  8.75 cc >1 mode
    

    I recommend that you use an editor with syntax highlighting and bracket matching, which would make it easier to find these sorts of syntax errors.

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

Sidebar

Related Questions

I have a large database of normalized order data that is becoming very slow
I have a large database and am putting together a report of the data.
I have large database table, approximately 5GB, now I wan to getCurrentSnapshot of Database
I have a large database and would like to select table names that have
I have a large database of users (~200,000) that I'm transferring from a ASP.NET
We have a large database containing a lot of stored procedures. Now we are
I have a large database table that I need to display on a Windows
I have a large database of resumes (CV), and a certain table skills grouping
I have a large database with over 150 tables that I've recently been handed.
Let's say I have a large database that consists of products in groups. Let's

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.