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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:18:00+00:00 2026-05-16T06:18:00+00:00

So I have a data frame in R that contains integers, NA’s, and a

  • 0

So I have a data frame in R that contains integers, NA’s, and a random assortment of strings inside the cells. Only one data type per cell. What I’m wondering is how to change all of the cells that contain strings into NA. Any idea how I could do this?

  • 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-16T06:18:01+00:00Added an answer on May 16, 2026 at 6:18 am

    If your data frame (df) is really all integers except for NAs and garbage then then the following converts it.

    df2 <- data.frame(lapply(df, function(x) as.numeric(as.character(x))))
    

    You’ll have a warning about NAs introduced by coercion but that’s just all those non numeric character strings turning into NAs.

    The following code also works and is more concise but runs slower.

    df2 <- apply(df, 2, function(x) as.numeric(as.character(x)))
    

    If you just want to convert selected columns then you could use a slightly more complicated command. First you need to figure out which columns you want to convert. Perhaps you save them as a logical vector of the columns you wish to change.

    df2 <- cbind(df[,!columnsToChange], apply(df[,columnsToChange], 2, function(x) as.numeric(as.character(x)))
    

    This would knock things out of order but it would get you what you want easy enough.

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

Sidebar

Related Questions

I have a data frame that contains a number of fields. One of these
I have a data.frame that contains historic data by day for several months. I
I have 2 frames. One contains buttons that do appropriate actions on data. And
i have a data frame that contains a data like this : V1 V2
I have a data frame that contains a long character string each associated with
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that has come about from running some
I have a data frame in R that looks like this: > TimeOffset, Source,
I have a data frame with two columns. First column contains categories such as
I have a data frame where one particular column has a set of specific

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.