if_na: Replace values with NA and vice-versa

Description Usage Arguments Format Value Examples

View source: R/if_na.R

Description

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
if_na(x, value, label = NULL)

if_na(x, label = NULL) <- value

x %if_na% value

na_if(x, value, with_labels = FALSE)

na_if(x, with_labels = FALSE) <- value

x %na_if% value

mis_val(x, value, with_labels = FALSE)

mis_val(x, with_labels = FALSE) <- value



valid(x)

Arguments

x

vector/matrix/data.frame/list

value

single value, vector of the same length as number of rows in x, or function (criteria) for na_if. See recode for details.

label

a character of length 1. Label for value which replace NA.

with_labels

logical. FALSE by default. Should we also remove labels of values which we recode to NA?

Format

An object of class character of length 1.

Value

object of the same form and class as x. valid returns logical vector.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# simple case
a = c(NA, 2, 3, 4, NA)
if_na(a, 99)

# the same result
a %if_na% 99

# with label
a = c(NA, 2, 3, 4, NA)
if_na(a, 99, label = "Hard to say")

# in-place replacement. The same result:
if_na(a, label = "Hard to say") = 99 
a # c(99, 2, 3, 4, 99)

# replacement with values from other variable
a = c(NA, 2, 3, 4, NA)
b = 1:5
if_na(a, b)

# replacement with group means
# make data.frame 
set.seed(123)
group = sample(1:3, 30, replace = TRUE)
param = runif(30)
param[sample(30, 10)] = NA # place 10 NA's
df = data.frame(group, param)

# replace NA's with group means
if_na(df$param) = window_fun(df$param, df$group, mean_col)
df

######################
### na_if examples ###
######################

a = c(1:5, 99)
# 99 to NA
na_if(a, 99)    # c(1:5, NA)

a %na_if% 99    # same result

# values which greater than 4 to NA
na_if(a, gt(4)) # c(1:4, NA, NA)

# alias 'mis_val', with_labels = TRUE
a = c(1, 1, 2, 2, 99)
val_lab(a) = c(Yes = 1, No = 2, "Hard to say" = 99)
mis_val(a, 99, with_labels = TRUE)

set.seed(123)
dfs = data.frame(
      a = c("bad value", "bad value", "good value", "good value", "good value"),
      b = runif(5)
)

# rows with 'bad value' will be filled with NA
# logical argument and recycling by columns
na_if(dfs, dfs$a=="bad value")

a = rnorm(50)
# values greater than 1 or less than -1 will be set to NA
# special functions usage
na_if(a, lt(-1) | gt(1))

# values inside [-1, 1] to NA
na_if(a, -1 %thru% 1)

Example output

[1] 99  2  3  4 99
[1] 99  2  3  4 99
VALUES:
99, 2, 3, 4, 99
VALUE LABELS:               
 99 Hard to say
VALUES:
99, 2, 3, 4, 99
VALUE LABELS:               
 99 Hard to say
[1] 1 2 3 4 5
   group      param
1      3 0.38461540
2      3 0.75845954
3      3 0.21640794
4      2 0.31818101
5      3 0.23162579
6      2 0.46211834
7      2 0.41454634
8      2 0.41372433
9      3 0.38461540
10     1 0.15244475
11     2 0.13880606
12     2 0.23303410
13     1 0.46596245
14     2 0.26597264
15     3 0.38461540
16     1 0.04583117
17     3 0.44220007
18     3 0.38461540
19     1 0.12189926
20     1 0.56094798
21     1 0.24576954
22     1 0.12753165
23     3 0.38461540
24     2 0.89504536
25     3 0.38461540
26     2 0.66511519
27     1 0.24576954
28     2 0.46211834
29     3 0.27438364
30     2 0.81464004
[1]  1  2  3  4  5 NA
[1]  1  2  3  4  5 NA
[1]  1  2  3  4 NA NA
VALUES:
1, 1, 2, 2, NA
VALUE LABELS:      
 1 Yes
 2 No 
           a         b
1  bad value 0.2875775
2  bad value 0.7883051
3 good value 0.4089769
4 good value 0.8830174
5 good value 0.9404673
 [1]          NA          NA -0.10896597 -0.11724196  0.18308261          NA
 [7]          NA          NA  0.50381245          NA  0.54909674  0.23821292
[13]          NA          NA  0.82553984 -0.05568601 -0.78438222 -0.73350322
[19] -0.21586539 -0.33491276          NA -0.08542326          NA -0.14539355
[25]          NA -0.81851572  0.68493608 -0.32005642          NA -0.59960833
[31] -0.12941069  0.88673615 -0.15139596  0.32979120          NA -0.77179177
[37]  0.28654857          NA  0.43455038  0.80017687 -0.16393097          NA
[43] -0.93438506  0.39370865  0.40363146 -0.88643672          NA  0.02884391
[49] -0.43212979          NA
 [1] -1.689556  1.239496        NA        NA        NA  1.280555 -1.727271
 [8]  1.690184        NA  2.528337        NA        NA -1.048893  1.294763
[15]        NA        NA        NA        NA        NA        NA -1.085699
[22]        NA  1.070611        NA -1.165545        NA        NA        NA
[29] -1.311522        NA        NA        NA        NA        NA -3.227323
[36]        NA        NA -1.220512        NA        NA        NA  1.242919
[43]        NA        NA        NA        NA -1.318938        NA        NA
[50]  1.689873

expss documentation built on Jan. 8, 2021, 5:38 p.m.