0

I have a csv file with daily price values listed by date. For some of the price values excel has no numerical data but a . instead (I believe this means that no price data were recorded for those dates. This file was imported to R and the price values that had a "." in Excel also have a "." in R.

I would like to replace "." with NA but the methods I have applied are not working.

I've tried to mutate the price vector and to replace the values in the price vector to NA.

Here are some of the steps I've tried:

Step 1:

btc %>% mutate(btc = if_else(BCUD == ".", NA))

Step 2:

replace(btc$BCUD, " ", NA)
Cettt
  • 11,460
  • 7
  • 35
  • 58
uThandi
  • 11
  • 1
  • Look at the docs for `if_else`. It takes 3 arguments: a predicate that evaluates to true or false, a value for if it's true, and a value for if it's false. You've given it 2 arguments. – camille Apr 15 '19 at 21:57
  • 1
    Do you read in the data using `read.csv()`? Try giving it the argument `na.strings = c("NA", ".")` – AkselA Apr 15 '19 at 22:02

2 Answers2

0

Here is an example how you can replace "." with NA:

library(tidyverse)
mydata <- tibble(x = c("a", ".", "b"))
mydata 
# A tibble: 3 x 1
      x
  <chr>
1     a
2     .
3     b

The second row contains ".". Here is how you get rid of it:

mydata %>% mutate(x = if_else(x == ".", NA_character_, x))
# A tibble: 3 x 1
      x
  <chr>
1     a
2  <NA>
3     b
Cettt
  • 11,460
  • 7
  • 35
  • 58
0

Try the following to replace "." with NA for a single variable

  btc$BCUD[btc$BCUD == "."] <- NA
PsychometStats
  • 340
  • 1
  • 7
  • 19