How can I remove the characters from the columns of a data frame?
Something like this for all:
mtcars %>% replace(is.na(.), 0)
Or specific columns:
Thank you for your response
The problem I am having is that the string is not just NA, but less than sign NA greater than sign as in the title of the question, so the is.na expression does not match.
This works very well and removes all the quotation marks:
df <- mutate_all(df,
funs(str_replace_all(., "\[|\]", "")))
but I cannot find a way to match less than symbolNAgreater than symbol
I am not very proficient with regular expressions. Do you think you can help me to find a regular expression for "<NA>"?
There is probably a more elegant solution, but something like
replace(mtcars$mpg, mtcars$mpg == "<NA>", 0)
Possibly something like this for all columns:
df %>% mutate_all(funs(str_replace(., "<NA>", NA_character_)))
Suppose these are the contents of a cell in the column of a dataframe:
1 \ <NA>
2 \ <NA>
3 \ <NA>
Is may be the problem that the <NA>\ are not in a single row but in rows 1, 2, 3, 4?
Nothing seems to work. However df <- mutate_all(df,
funs(str_replace_all(., "\[|\]", ""))) works very well to remove the brackets
df <- mutate_all(df, funs(str_replace(.,"\<|N|A|\>", ""))) does not work.
The only difference is that in the data frame column for the case of the brackets, there is only 1 row\ [34.5][23.4]....., but in the <NA> column there are several rows.
3 <NA> and so own. I wonder if the is the reason why replace function does not work for <NA>.
library(tidyverse) # example df df <- tibble(a = c("A", "B", "<NA>", "<NA>"), b = c("<NA>", "C", "D", "E")) # this works df %>% mutate_all(~(str_replace(., "<NA>", NA_character_))) # A tibble: 4 × 2 a b <chr> <chr> 1 A NA 2 B C 3 NA D 4 NA E
How can I make a tible out of my data frame?
You can use
dput(head(your_df)) or even better, use datapasta::dpasta()
Thank you very much for this. It is awesome.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.