[R] Replacing NA's in the data set
arun
smartpink111 at yahoo.com
Tue Apr 29 16:34:00 CEST 2014
Hi Dila,
Try:
Ts1 <- Ts
Ts[,-11] <- lapply(Ts[,-11], function(x) {x[is.na(x)] <- Ts[is.na(x), 11]; x})
#or
Ts1[,-11][is.na(Ts1[,-11])] <-rep(Ts1[,11],10)[is.na(Ts1[,-11])]
identical(Ts,Ts1)
#[1] TRUE
A.K.
On Tuesday, April 29, 2014 8:53 AM, dila radi <dilaradi21 at gmail.com> wrote:
Hi all,
I have this data set,
> dput(Ts)
structure(list(V1 = c(3, 17, 29, 12, 4.5, 1, 0.5, 0, 0, 0, 1.2,
1.8, 1.5, 0.5, 0, 47.7, 0.3, 0, 2, 0, 5.5, 8, 27.5, 69, 29, 24.5,
57.5, 40, 1, 14.5, 0, 0, 0, 0, 0, 0, 1.5, 2, 0, 0, 0, 0, 7, 29.5,
77, 11.5, 33, 38, 36, 8, 28, 11, 11, 0, 17, 0, 51, 0.5, 5, 0,
31.5, 11, 3.5, 0, 0, 0, 0, 0, 4.5, 1, 15.5, 2.5, 0, 0, 5, 0,
0, 0, 0, 0, 5, 0, 8.5, 0, 0, 0, 0, 0, 0, 13, 37.5, 3.5, 37, 2,
32.5, 2.5, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 105.5, 0.5, 1.5,
16.5, 5.5, 0, 0, 0, 3.5, 0, 0, 10.5, 57.5, 3, 23, 0, 3, 1, 43.5,
39.5, 1, 0, 21, 2.5, 2.5, 1.5, 30, 2, 1.5, 18.5, 17, 0, 0, 13,
0, 0, 0, 0, 5.5, 0, 0, 0, 0, 22.5, 4.5, 0, 1.5, 17, 1.5, 0.5,
2, 0, 0, 25, 24.5, 7.5, 0, 3.5, 22.5, 8.5, 8.5, 0, 0, 0, 0.5,
0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 4.5, 0, 49, 0, 2,
12, 29, 6.5, 0, 0, 0, 0, 2.5, 0, 0, 6, 0, 8.5, 8.5, 8.5, 1, 0,
0), V2 = c(4, 12, 22.5, 10.5, 19.5, 1, 0.5, 0, 0, 0, 3, 0, 0,
3, 0, 8, 0, 0, 0, 1, 2.5, 10.5, 21, 49.5, 79.5, 16, 30, 34, 0,
13.5, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0.5, 3.5, 7, 4.5,
2.5, 2, 22.5, 8, 99.5, 10, 44, 0, 3.5, 0, 18.5, 0, 5, 11.5, 0,
18.5, 4.5, 0, 0, 0, 0.5, 0, 5.5, 3.5, 5.5, 87, 2, 2, 0, 1, 0,
0, 0, 0, 0, 0, 0, 13.5, 0, 0, 12, 8, 1, 0, 0, 3.5, 0, 1, 5, 0,
0.5, 0.5, 0, 0, 0, 0, 0, 1, 0, 0, 11, 0, 5.5, 0, 0, 2, 25.5,
0, 17.5, 0, 1, 0, 0, 3, 44.5, 0, 0, 0.3, 43.7, 0, 3.2, 13.5,
2, 0, 0, 0, 0, 5.8, 16.2, 0, 0, 7.3, 0, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, 0,
0, 14.9, 16.1, 0, 16.8, 2.2, 15.1, 92.7, 0, 0, 1.6, 36.2, NA,
NA, NA, NA, NA, NA, NA, NA, 1, 0, 0, 0, 17, 1, 0, 0, 3.5, 0.5,
25, 10.5, 28, 6.5, 0, 8, 1, 16, 0, 0, 0, 2.5, 0, 0, 61, 0, 0,
0, 8.5), V3 = c(11L, 21L, 41L, 22L, 0L, 0L, 12L, 0L, 0L, 3L,
4L, 7L, 0L, 3L, 5L, 19L, 0L, 0L, 0L, 0L, 23L, 36L, 57L, 34L,
123L, 31L, 56L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0L), V4 = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, 6.5, 0, 0, 0, 3.5, 0.5, 26, 12, 0, 1.5,
2, 6.5, 34.5, 40, 61, 179, 19, 44, 48.5, 0.5, 0, 1.5, NA, NA,
NA, NA, NA, NA, NA, NA, 0, 0.5, 0, 20, 15.5, 16, 3.5, 13, 5,
5.5, 15, 6.5, 2.5, 93.5, 1.5, 9.5, 3, 9, 0, 0, 0, 0, 2, 3.5,
0, 0, 0, 3, 0, 9.5, 7.5, 3.5, 57, 2.2, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.5, 6, 0, 0,
0.5, 0, 0, 0, 10, 0, 0, 6, 0, 0, 0, 6, 0, 6.5, 1.5, 0, 0, 32.5,
0, 0, 0, 15, 0, 0, 0, 8.5, 0, 53, 0, 27.5, 0, 12.5, 2.3, 3.2,
0, 0, 0, 1, 2, 0.5, 3, 9.5, 20.5, 12.5, 3.5, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 104, 2.5, 0, 0, 0, 0, 42, 0, 11.5, 41,
0, 14, 0, 18, 8, 0, 0, 14, 14.5, 0, 13.5, 0, 0, 5.5, 2, 0, 1,
0, 0, 0, 0, 3.5, 0, 0, 37.5, 0, 0, 39.2, 9.8, 0, 0, 0, 0, 0,
40.5, 0, 2, 0, 0, 2, 16, 3, 0, 1, 0, 4), V5 = c(28.5, 24, 45.5,
12.5, 0, 0, 0, 10.5, 7, 0, 0, 0, 6, 0, 38, 0, 1.5, 2, 3, 10,
32.5, 132, 20.5, 190, 19, 84, 120.5, 2, 4, 12, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 9, 12, 7.5, 11, 9.5, 0, 11, 8, 12, 11, 0, 86,
0, 0, 0, 10, 0, 0, 0, 1, 5, 1, 0, 0, 0, 0, 0, 10, 49.5, 0, 36.5,
0, 0, 0, 0, 0, 0, 0, 0, 2.5, 0, 0, 0, 0, 0, 75, 4, 0, 0, 0, 0,
0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7.5, 0, 0, 9, 1, 0,
7, 0, 0, 0, 25, 8, 0, 0, 30, 0, 0, 4, 0, 0, 41, 11, 0, 20, 0,
0, 0, 0, 0, 0, 0, 57, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, 0, 0,
0, 0, 0, 6.5, 0, 0, 0, 44.5, 0, 0, 17.5, 2, 0, 0, 0, 18, 0, 0,
14.5, 0, 0, 0, 15.5, 3, 1, 0, 0, 0, 0), V6 = c(57.5, 35.5, 60,
12.5, 4, 0, 0, 6.5, 11, 5, 7.5, 4.5, 5.5, 4, 4.5, 0, 63.5, 7,
0, 14.5, 34.5, 44.5, 33.5, 229, 16, 103.5, 102, 12.5, 17, 14.5,
0, 5.5, 0, 0, 0, 0, 4, 0, 0, 0, 6, 8.5, 0, 36.5, 13, 4, 3, 4,
6, 56, 4, 27.5, 6, 0, 7, 9, 0, 6.5,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 0, 0, 0, 0, 0, 0, 23, 0, 0, 30, 0, 0, 2.5,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 15, 0, 0, 0, 0, 0, 0, 0,
0, 7.5, 2, 0, 0, 5, 14.5, 1.5, 1, 0, 0, 0, 0, 21, 9, 2.5, 4,
7, 0, 0, 0, 8, 1.5, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 59.5, 0, 40, 0, 0, 0, 145.5, 7.5, 0, 0, 11, 7, 0, 0,
52.5, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 9.5, 9.5,
0, 2.5, 4, 3.5, 65.5, 0, 16.5, 6, 0, 0, 15, 0, 1.5, 0, 0, 0,
0, 1, 0, 0, 0, 0, 10, 48), V11 = c(48.16666667, 34.55555556,
39.38888889, 19.27777778, 6.125, 1.3125, 4.5, 2.75, 11.75, 3.777777778,
2.411111111, 4.588888889, 7.944444444, 2, 5.333333333, 11.35555556,
8.588888889, 7, 4.111111111, 10.66666667, 27.11111111, 44.27777778,
33.5, 79.66666667, 55.88888889, 36.38888889, 57.16666667, 24.4375,
7.25, 13.25, 4.4375, 1.428571429, 1.142857143, 0, 0, 0, 2.428571429,
0.285714286, 0, 0, 21.1875, 5.75, 7.5, 15.8125, 20.1875, 5.8125,
9.875, 8.1875, 13.375, 21.4375, 26.375, 17.8125, 25.5625, 0.3125,
10.38888889, 5.4, 9.033333333, 4, 1.166666667, 2.555555556, 8.644444444,
7.144444444, 4.444444444, 0.855555556, 0.055555556, 0.055555556,
0.388888889, 7.833333333, 32.03333333, 38.4, 25.04444444, 31.48888889,
3.055555556, 0.3875, 0.7125, 0.3125, 0, 0, 0.5, 0.375, 0.8125,
0.75, 4.0625, 2.0125, 0, 21.375, 15.225, 3.55, 0.875, 11.9875,
5.825, 1.488888889, 9.844444444, 4.933333333, 4.333333333, 0.777777778,
0.388888889, 7.277777778, 0.544444444, 1.133333333, 0, 0, 0.666666667,
0.111111111, 0, 1.444444444, 3.222222222, 0.833333333, 15.83333333,
0.655555556, 0.777777778, 7.344444444, 8.455555556, 1.222222222,
2, 2.777777778, 3.333333333, 0.833333333, 0.777777778, 10.2,
17.88888889, 10.57777778, 11.65555556, 2.866666667, 10.17777778,
8, 17.26666667, 13.94444444, 6.566666667, 3.533333333, 3.844444444,
3.555555556, 6.4, 3.6, 5.4, 0.555555556, 11.22222222, 16.42222222,
5.777777778, 2, 0, 1.625, 0, 0, 0, 0.125, 5, 0.357142857, 0.214285714,
0.142857143, 0.642857143, 3.5, 0.785714286, 0.928571429, 22.48571429,
12.21428571, 0.857142857, 5.785714286, 0.285714286, 3.4375, 5.625,
28, 20.8, 10.85, 4.8875, 15.1875, 11.65, 9.975, 22.4125, 6.5625,
6.875, 5.325, 7.125, 1, 1.928571429, 2.5, 1.4, 1.642857143, 0.675,
2.6875, 3.275, 0.888888889, 0, 0, 2.944444444, 4.444444444, 2.444444444,
1.166666667, 14.5, 13.66666667, 9.666666667, 13.3, 12.03333333,
12.38888889, 1.677777778, 0.5, 5.222222222, 1.055555556, 6.988888889,
3.088888889, 0.822222222, 0.777777778, 1.333333333, 4.555555556,
10.11111111, 15.22222222, 6.155555556, 0.688888889, 3.555555556,
10.12)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7",
"V8", "V9", "V10", "V11"), class = "data.frame", row.names = c(NA,
-210L))
I want to replace all NA's value in column 1 until column 10 with the
corresponding value in column 11. How to achieve this. Please help me.
Thank you.
Dila
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list