fill_na: Fill 'NA' in data.frame with values from another column

Description Usage Arguments Value Examples

View source: R/fill_na.R

Description

fill_na fills in any NA rows in a data.frame column with the corresponding values from another column in the same data.frame. It will coerce the column with NA into the same type as the column used to fill.

This function does not work well with factors.

Usage

1
fill_na(df, na_col, fill_col)

Arguments

df

data.frame

na_col

column in df: contains NA values

fill_col

column in df: ccontains values to replace NA values

Value

Returns a data.frame with NA in a specific column filled with values from another column.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# make data.frame with NA
my_data <- data.frame(name1 = c("Bob", "Jane", "Henry", "Sue"),
                      name2 = c("Mary", NA, "Tim", NA),
                      stringsAsFactors = FALSE)

# fill NA in names2
result <- fill_na(df = my_data, na_col = "name2", fill_col = "name1")

# make a another data.frame with NA
my_data <- data.frame(num1 = c(1, 2, 3),
                      num2 = c(3, 3, NA))

# fill NA in num2
result <- fill_na(df = my_data, na_col = "num2", fill_col = "num1")

csdaw/csdata documentation built on April 23, 2020, 10:48 p.m.