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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:55:29+00:00 2026-06-13T06:55:29+00:00

I have a matrix with some NA that I’d like to format and then

  • 0

I have a matrix with some NA that I’d like to format and then write to a CSV file, replacing each NA with two dashes. However, all of the NA get converted to characters by format, and write.csv then fails to detect and replace them.

After reading through the help file for format, setting na.encode=FALSE seemed like the logical thing to do, but that didn’t fix the problem. I’ve also searched with Google and on this site for other people having this problem, but couldn’t find any. This seems like it should have a simple answer, and has me beating my head against the wall.

A minimal example of my code looks like this:

data = matrix(c(pi, NA, pi*100, NA), 2, 2)
data.f = format(data, digits=4, nsmall=2)
write.csv(data.f, file="data.csv", na="--")

I’m using R 2.15.1. What is the correct way to do this?

Edit: Can anyone explain why this even requires a workaround?

  • 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-13T06:55:30+00:00Added an answer on June 13, 2026 at 6:55 am

    There’s probably a more orthodox way to do this, but perhaps you can use !is.na() at the format() stage to prevent the NA values from being converted to characters.

    Here’s a minimal example along with the resulting CSV.

    set.seed(1)
    data <- matrix(rnorm(20), ncol=5)
    data[sample(length(data), 5)] <- NA
    data.f <- data
    data.f[!is.na(data.f)] <- format(data.f[!is.na(data.f)], digits = 4, nsmall = 2)
    write.csv(data.f, file = "data.csv", na = "--")
    dput(write.csv(data.f, na = "--"))
    # "","V1","V2","V3","V4","V5"
    # "1","-0.62645"," 0.32951",--,--,--
    # "2"," 0.18364","-0.82047",--,"-2.21470"," 0.94384"
    # "3","-0.83563"," 0.48743"," 1.51178",--," 0.82122"
    # "4"," 1.59528"," 0.73832"," 0.38984","-0.04493"," 0.59390"
    # NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supose I have a matrix. This matrix is blank except some points that create
I have a .rdlc report and it has matrix. that matrix has some money
I have a huge matrix that I divided it into some sub matrices and
I have noticed that matlab does some matrix function really fast for example adding
I have some code that I have inherited that generates a matrix of significance
I have this matrix of size 10000 by 1. I want to remove some
I have matrix (a) with (1:10),<10 x 1> double. I would like to copy
Have a matrix report now that has Position, Hours and Wages for a location
I have a matrix of objects that contains data in this form: name A,2,name
I have some memory that has been allocated on device that is just a

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.