Search
 
SCRIPT & CODE EXAMPLE
 

R

r replace na with 0

d[is.na(d)] <- 0
Comment

replace na with 0 in R

library(dplyr)
#Replacing missing values with 0 in columns 'x' and 'y' of the tibble dataframe 'df'
df %>% 
  replace_na(list(x = 0, y = 0))
Comment

r replace na with 0

> m <- matrix(sample(c(NA, 1:10), 100, replace = TRUE), 10)
> d <- as.data.frame(m)
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1   4  3 NA  3  7  6  6 10  6   5
2   9  8  9  5 10 NA  2  1  7   2
3   1  1  6  3  6 NA  1  4  1   6
4  NA  4 NA  7 10  2 NA  4  1   8
5   1  2  4 NA  2  6  2  6  7   4
6  NA  3 NA NA 10  2  1 10  8   4
7   4  4  9 10  9  8  9  4 10  NA
8   5  8  3  2  1  4  5  9  4   7
9   3  9 10  1  9  9 10  5  3   3
10  4  2  2  5 NA  9  7  2  5   5

> d[is.na(d)] <- 0

> d
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1   4  3  0  3  7  6  6 10  6   5
2   9  8  9  5 10  0  2  1  7   2
3   1  1  6  3  6  0  1  4  1   6
4   0  4  0  7 10  2  0  4  1   8
5   1  2  4  0  2  6  2  6  7   4
6   0  3  0  0 10  2  1 10  8   4
7   4  4  9 10  9  8  9  4 10   0
8   5  8  3  2  1  4  5  9  4   7
9   3  9 10  1  9  9 10  5  3   3
10  4  2  2  5  0  9  7  2  5   5
Comment

How to replace values with na in r

# Change al "" (blank spaces) to NA values
library(tidyverse)
data <- data %>%
  mutate(across(where(is.character), ~na_if(., "")))
Comment

PREVIOUS NEXT
Code Example
R :: find length of a list or vector in r 
R :: how to read csv file in r 
R :: r ddply 
R :: export csv file in r 
R :: delete all rows that contain a string in R 
R :: r - reorder columns in data frame 
R :: describe data in r 
R :: change all columns type in R 
R :: string concatination R 
R :: how to create a loop for different categories in a column in r 
R :: reduce ggtitle size 
R :: dataframe to r code 
R :: how to use r with variable 
R :: r tibble select rows containing string 
R :: extract attribute in r 
R :: dplyr average columns 
R :: r select columns by vector of names 
R :: Levels in factor in r 
R :: how to exclude inf in r 
R :: base R change axis line width 
R :: decompose function in r 
R :: r alluvial chart with NA 
Rust :: read file buffer rust 
Rust :: rust check valid email address using regex 
Rust :: rust square root 
Rust :: armanriazi•rust•error•E0282•type annotations needed 
Rust :: armanriazi•rust•syntax•names 
Rust :: control flow rust 
Rust :: rust absolute path 
Rust :: find prime numbers with the sieve of Eratosthenes 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =