0

i am trying to replace all the question mark in a data frame ,so i can do future data preprocessing,I tried sevral methods but all won't work.

i tried to traverse all the data in data frame and replace "?" with NA(that is what i used to do in python)

  replace.question.mark <- function (df) {
    for(i in 1:nrow(df)){
       for(ii in 1:ncol(df)){
          df[i,ii]<-replace(df[i,ii],df[i,ii]=="?",NA)
        }
     }
  }

I expect to replace all the "?" with NA,but the output is just null

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
jimmy jiang
  • 47
  • 1
  • 7
  • 1
    Could you show what your data looks like and what you would like to obtain? There might be a simpler way. – NelsonGon Apr 21 '19 at 02:49
  • 1
    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Apr 21 '19 at 02:55
  • 2
    `dummy_df[dummy_df == '?'] <- NA` – Ronak Shah Apr 21 '19 at 03:13

1 Answers1

0

Try this?

df1 = data.frame(x1 = c("a","?","b"),x2= c("d","e","?"))

df1 %>% 
  map_df(.,.f=function(x){
             ifelse(as.character(x) == "?",
             NA,as.character(x))})
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
jsv
  • 740
  • 3
  • 5