Reputation:
> a
deptime total
1 NA 341
2 1 2
3 4 1
4 5 1
5 6 1
6 8 1
7 9 2
8 10 1
9 11 1
10 15 2
11 17 2
12 20 1
13 21 2
14 23 1
15 25 3
16 28 1
17 29 5
18 30 2
19 31 1
20 33 1
21 36 2
22 38 1
23 39 2
24 41 1
25 44 2
26 45 2
27 50 1
28 51 2
29 52 2
30 54 1
31 57 1
32 58 1
33 103 1
34 104 1
35 105 1
36 107 1
37 114 1
38 119 1
39 126 1
40 131 1
41 136 1
42 148 1
43 158 1
44 208 1
45 230 1
46 244 1
47 410 1
48 418 1
49 441 1
50 446 1
51 452 2
52 455 2
53 456 1
54 500 3
55 501 1
56 505 1
57 507 1
58 508 1
59 511 1
60 515 2
61 516 2
62 517 1
63 518 3
64 519 2
65 520 4
Upvotes: 0
Views: 20
Reputation: 269885
1) The zoo package has na.fill
for this purpose. It is non-destructive:
library(zoo)
a0 <- na.fill(a, fill = 0) # replace NAs in all columns
or if you only want to fill the first column:
a$deptime <- na.fill(a$deptime, fill = 0)
2) Without zoo:
a[is.na(a)] <- 0 # replace NAs in all columns
or just the deptime
column:
a$deptime[is.na(a$deptime)] <- 0
This also just does the replacement in the deptime
column and is non-destructive:
a0 <- transform(a, deptime = replace(deptime, is.na(deptime), 0))
Upvotes: 0
Reputation: 6784
a[1,"deptime"] <- 0
or
a[is.na(a$deptime),]$deptime <- 0
or many other variants of these
Upvotes: 1