merged$number_articles %>% replace_na(0)
Im trying to replace all na values in a column (number_articles) in the df (merged), with 0. I have tried the above code but the values are not getting replaced. It still shows NA in every observation. Also, I have to do this using dplyr.
I know this isn't quite what you are looking for, but you can do this in base R with:
merged$number_articles[is.na(merged$number_articles)] <- 0
take a look at?replace_na for some help.
This should work:
merged <- merged %>%
mutate(number_articles = replace_na(number_articles, 0))
EDIT: modified to include in a mutate, which I believe solves the issue identified by 42 in comments.
R generally (with the data.table exception) requires replacing the starting point with the result of the computation. After reviewing the examples in help(replace_na, pac=tidyr), I suggest:
library(tidyr) # or library(tidyverse)
merged <- merged %>%
replace_na( list(number_articles =0) )
Or:
merged <- merged %>% mutate(
replace_na( number_articles , 0) )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.