Matrix.utils merge.Matrix and merge different results - r

I am very confused by the R package Matrix.utils and its implementation of merge.Matrix(). I want to merge two matrices with 0 common values, but merge common column names and fill the rest with zeros.
The results are inconsistent and sensitive to whether merge() or merge.Matrix() is specified. I expected this to be similar to the dplyr::join() function but this is not true.
Simulating the data I plan to use:
mtx.x <- sample(1:100, 100) ; mtx.x <- matrix(mtx.x, nrow = 10)
mtx.y <- sample(1:100, 100) ; mtx.y <- matrix(mtx.y, nrow = 10)
colnames(mtx.x) <- letters[1:10] ; colnames(mtx.y) <- letters[6:15]
mtx.x ; mtx.y
a b c d e f g h i j
[1,] 82 61 76 36 27 67 85 38 29 87
[2,] 83 89 43 70 81 30 35 17 39 95
[3,] 1 75 69 54 66 3 10 47 93 73
[4,] 52 98 26 88 51 64 31 72 13 92
[5,] 44 74 86 9 63 58 50 56 6 49
[6,] 24 16 77 12 55 97 18 45 14 40
[7,] 11 5 79 94 2 80 37 15 41 42
[8,] 100 84 65 59 34 62 53 60 99 28
[9,] 19 78 8 25 96 21 90 46 68 71
[10,] 32 20 7 4 57 91 22 48 33 23
f g h i j k l m n o
[1,] 24 22 8 94 89 7 50 93 40 4
[2,] 63 80 32 44 64 83 16 96 46 47
[3,] 85 30 81 95 23 91 19 92 99 52
[4,] 21 55 61 58 27 76 67 65 37 14
[5,] 9 66 12 2 41 11 56 84 87 39
[6,] 18 57 88 3 68 100 74 62 82 25
[7,] 70 90 43 54 72 86 69 20 29 51
[8,] 1 59 60 45 79 75 15 5 73 10
[9,] 38 28 26 17 53 36 97 13 77 49
[10,] 6 71 98 35 42 31 78 33 48 34
Case 1: merge() with all.x/all.y set to TRUE does what I want
merge(x = mtx.x, y = mtx.y,
all.x = T, all.y = T)
f g h i j a b c d e k l m n o
1 1 59 60 45 79 NA NA NA NA NA 75 15 5 73 10
2 3 10 47 93 73 1 75 69 54 66 NA NA NA NA NA
3 6 71 98 35 42 NA NA NA NA NA 31 78 33 48 34
4 9 66 12 2 41 NA NA NA NA NA 11 56 84 87 39
5 18 57 88 3 68 NA NA NA NA NA 100 74 62 82 25
6 21 55 61 58 27 NA NA NA NA NA 76 67 65 37 14
7 21 90 46 68 71 19 78 8 25 96 NA NA NA NA NA
8 24 22 8 94 89 NA NA NA NA NA 7 50 93 40 4
9 30 35 17 39 95 83 89 43 70 81 NA NA NA NA NA
10 38 28 26 17 53 NA NA NA NA NA 36 97 13 77 49
11 58 50 56 6 49 44 74 86 9 63 NA NA NA NA NA
12 62 53 60 99 28 100 84 65 59 34 NA NA NA NA NA
13 63 80 32 44 64 NA NA NA NA NA 83 16 96 46 47
14 64 31 72 13 92 52 98 26 88 51 NA NA NA NA NA
15 67 85 38 29 87 82 61 76 36 27 NA NA NA NA NA
16 70 90 43 54 72 NA NA NA NA NA 86 69 20 29 51
17 80 37 15 41 42 11 5 79 94 2 NA NA NA NA NA
18 85 30 81 95 23 NA NA NA NA NA 91 19 92 99 52
19 91 22 48 33 23 32 20 7 4 57 NA NA NA NA NA
20 97 18 45 14 40 24 16 77 12 55 NA NA NA NA NA
Case 2: merge.Matrix() with same arguments wants me to specify by.x/by.y
merge.Matrix(x = mtx.x, y = mtx.y,
all.x = T, all.y = T)
Error in grr::matches(by.x, by.y, all.x, all.y, nomatch = NULL) :
argument "by.x" is missing, with no default
Case 3: specifying by.x/by.y as respective column names does not merge common columns. also, no idea why its offsetting the matrices by 5 and not 10, the matrices have no common values.
merge.Matrix(x = mtx.x, y = mtx.y,
all.x = T, all.y = T,
by.x = colnames(mtx.x), by.y = colnames(mtx.y))
a b c d e f g h i j y.f y.g y.h y.i y.j k l m n o
82 61 76 36 27 67 85 38 29 87 NA NA NA NA NA NA NA NA NA NA
83 89 43 70 81 30 35 17 39 95 NA NA NA NA NA NA NA NA NA NA
1 75 69 54 66 3 10 47 93 73 NA NA NA NA NA NA NA NA NA NA
52 98 26 88 51 64 31 72 13 92 NA NA NA NA NA NA NA NA NA NA
44 74 86 9 63 58 50 56 6 49 NA NA NA NA NA NA NA NA NA NA
24 16 77 12 55 97 18 45 14 40 24 22 8 94 89 7 50 93 40 4
11 5 79 94 2 80 37 15 41 42 63 80 32 44 64 83 16 96 46 47
100 84 65 59 34 62 53 60 99 28 85 30 81 95 23 91 19 92 99 52
19 78 8 25 96 21 90 46 68 71 21 55 61 58 27 76 67 65 37 14
32 20 7 4 57 91 22 48 33 23 9 66 12 2 41 11 56 84 87 39
fill.x NA NA NA NA NA NA NA NA NA NA 18 57 88 3 68 100 74 62 82 25
fill.x NA NA NA NA NA NA NA NA NA NA 70 90 43 54 72 86 69 20 29 51
fill.x NA NA NA NA NA NA NA NA NA NA 1 59 60 45 79 75 15 5 73 10
fill.x NA NA NA NA NA NA NA NA NA NA 38 28 26 17 53 36 97 13 77 49
fill.x NA NA NA NA NA NA NA NA NA NA 6 71 98 35 42 31 78 33 48 34
Case 4: by.x/by.y specified as common column names, all.x/all.y set to TRUE and fill.x/fill.y set to 0 does not do a full join as the documentation claims
common <- intersect(colnames(mtx.x), colnames(mtx.y))
merge.Matrix(x = mtx.x, y = mtx.y,
all.x = T, all.y = T,
by.x = common, by.y = common)
a b c d e f g h i j y.f y.g y.h y.i y.j k l m n o
82 61 76 36 27 67 85 38 29 87 24 22 8 94 89 7 50 93 40 4
83 89 43 70 81 30 35 17 39 95 63 80 32 44 64 83 16 96 46 47
1 75 69 54 66 3 10 47 93 73 85 30 81 95 23 91 19 92 99 52
52 98 26 88 51 64 31 72 13 92 21 55 61 58 27 76 67 65 37 14
44 74 86 9 63 58 50 56 6 49 9 66 12 2 41 11 56 84 87 39

Related

How can I replace NAs in multiple columns with the values from other corresponding multiple columns in the same data set?

I am trying replace NA's over multiple columns with corresponding values from other columns in the df.
df = data.frame(ID = sample(1000:9999,10),
Age = sample(18:99,10),
Gender = sample(c("M","F"),10, replace = TRUE),
Test1 = sample(60:100,10),
Test2 = sample(60:100,10),
Test3 = sample(60:100,10),
Test1.x = rep(NA,10),
Test2.x = rep(NA,10),
Test3.x = rep(NA,10))
df$Test1[c(2,3,8)] = NA
df$Test2[c(4,10)] = NA
df$Test3[c(1,7)] = NA
df$Test1.x[c(2,3,4,8)] = sample(60:100,4)
df$Test2.x[c(4,9,10)] = sample(60:100,3)
df$Test3.x[c(1,6,7)] = sample(60:100,3)
print(df)
ID Age Gender Test1 Test2 Test3 Test1.x Test2.x Test3.x
1 7877 40 M 78 70 NA NA NA 84
2 6345 54 F NA 99 61 62 NA NA
3 9170 41 F NA 80 96 82 NA NA
4 2400 83 M 100 NA 100 94 95 NA
5 5920 66 M 77 62 69 NA NA NA
6 2569 34 M 99 96 81 NA NA 100
7 7879 28 M 64 71 NA NA NA 90
8 8652 53 F NA 74 89 95 NA NA
9 6357 97 F 92 86 83 NA 86 NA
10 1943 45 M 95 NA 98 NA 72 NA
I would like to replace only the NAs in the test scores with the corresponding test.x score, while using str_replace. My actual data frame contain more than 3 columns but all the corresponding column names are the same with the ".x" afterwards.
Any ideas to make this quick and easy? I'm struggling between mutating across said columns or using replace_nas.
Within dplyr we could use coalesce with across.
library(dplyr)
df |>
mutate(across(starts_with("Test") & !ends_with(".x"),
~ coalesce(., get(paste0(cur_column(), ".x")))))
Output:
ID Age Gender Test1 Test2 Test3 Test1.x Test2.x Test3.x
1 5022 90 M 94 68 79 NA NA 79
2 1625 41 M 71 66 89 71 NA NA
3 6438 86 M 86 94 94 86 NA NA
4 3249 93 F 74 90 76 68 90 NA
5 7338 70 F 64 63 70 NA NA NA
6 9416 27 F 78 74 75 NA NA 64
7 4374 45 F 82 100 60 NA NA 60
8 6226 21 F 61 82 63 61 NA NA
9 5265 97 M 83 83 68 NA 89 NA
10 5441 95 M 70 79 99 NA 79 NA
Update 2/9:
To allow for other variable names we could do a specific solution or a more general one:
Specific:
df |>
mutate(across(c(HW, Exam, Final) & !ends_with(".x"),
~ coalesce(., get(paste0(cur_column(), ".x")))))
General:
df |>
mutate(across(ends_with(".x"),
~ coalesce(get(sub("\\.x", "", cur_column())), .)))
New output:
ID Age Gender HW Exam Final HW.x Exam.x Final.x
1 5166 80 F 60 79 NA 60 79 64
2 3375 35 M NA 88 72 65 88 72
3 5722 19 F NA 65 75 81 65 75
4 3701 27 M 89 NA 61 89 89 61
5 1424 67 F 69 94 91 69 94 91
6 1407 20 F 75 72 66 75 72 66
7 2927 39 M 63 82 NA 63 82 86
8 7315 90 F NA 92 79 70 92 79
9 7420 76 F 87 83 87 87 83 87
10 9334 73 F 86 NA 64 86 82 64
New data:
df = data.frame(ID = sample(1000:9999,10),
Age = sample(18:99,10),
Gender = sample(c("M","F"),10, replace = TRUE),
HW = sample(60:100,10),
Exam = sample(60:100,10),
Final = sample(60:100,10),
HW.x = rep(NA,10),
Exam.x = rep(NA,10),
Final.x = rep(NA,10))
df$HW[c(2,3,8)] = NA
df$Exam[c(4,10)] = NA
df$Final[c(1,7)] = NA
df$HW.x[c(2,3,4,8)] = sample(60:100,4)
df$Exam.x[c(4,9,10)] = sample(60:100,3)
df$Final.x[c(1,6,7)] = sample(60:100,3)
Using dplyover
library(dplyover)
df <- df %>%
mutate(across2(matches("Test\\d+$"), ends_with(".x"),
coalesce, .names = "{xcol}"))
-output
df
ID Age Gender Test1 Test2 Test3 Test1.x Test2.x Test3.x
1 7877 40 M 78 70 84 NA NA 84
2 6345 54 F 62 99 61 62 NA NA
3 9170 41 F 82 80 96 82 NA NA
4 2400 83 M 100 95 100 94 95 NA
5 5920 66 M 77 62 69 NA NA NA
6 2569 34 M 99 96 81 NA NA 100
7 7879 28 M 64 71 90 NA NA 90
8 8652 53 F 95 74 89 95 NA NA
9 6357 97 F 92 86 83 NA 86 NA
10 1943 45 M 95 72 98 NA 72 NA

How can you write an R code to print a number pattern in rows such as the following:

What functions would you need to use to write a program in R to print the following for n rows:
[1] 1
[1] 2 3
[1] 4 5 6
[1] 7 8 9 10
[1] 11 12 13 14 15
[1] ....
I have tried using for() to create a loop:
triangle=c()
for(i in 2:100){for(j in 2:i-1){triangle=c(triangle,j)}
print(triangle)
triangle=c()}
but each line starts on 1 and ends at 100 rows which is where I am stuck.
I am a beginner in R so please answer in the simplest terms! :)
I am not sure when do you want to break the loop but here is an attempt using while condition :
i <- 1
num <- 0
while(i <= 100) {
n <- i + num
cat(i:n, '\n')
i <- n + 1
num <- num + 1
}
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 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
This prints until the first number in the sequence is less than equal to 100.
To print alternate reverse sequence :
i <- 1
num <- 0
while(i <= 100) {
n <- i + num
if(num %% 2 == 0) cat(n:i, '\n')
else cat(i:n, '\n')
i <- n + 1
num <- num + 1
}
1
2 3
6 5 4
7 8 9 10
15 14 13 12 11
16 17 18 19 20 21
28 27 26 25 24 23 22
29 30 31 32 33 34 35 36
45 44 43 42 41 40 39 38 37
46 47 48 49 50 51 52 53 54 55
66 65 64 63 62 61 60 59 58 57 56
67 68 69 70 71 72 73 74 75 76 77 78
91 90 89 88 87 86 85 84 83 82 81 80 79
92 93 94 95 96 97 98 99 100 101 102 103 104 105
Or another possibility using sum(1:n)=n*(n+1)/2
for (n in 1:10) {
print(seq((n-1) * n / 2 + 1 , n * (n+1) / 2))
}
[1] 1
[1] 2 3
[1] 4 5 6
[1] 7 8 9 10
[1] 11 12 13 14 15
[1] 16 17 18 19 20 21
[1] 22 23 24 25 26 27 28
[1] 29 30 31 32 33 34 35 36
[1] 37 38 39 40 41 42 43 44 45
[1] 46 47 48 49 50 51 52 53 54 55
[1] 56 57 58 59 60 61 62 63 64 65 66
or reversed every second line:
for (n in 1:10) {
if (n%%2 ==1) {
print(seq((n-1)*n/2+1 , n*(n+1)/2))
} else {
print(seq(n*(n+1)/2, (n-1)*n/2+1 ))
}
}
[1] 1
[1] 3 2
[1] 4 5 6
[1] 10 9 8 7
[1] 11 12 13 14 15
[1] 21 20 19 18 17 16
[1] 22 23 24 25 26 27 28
[1] 36 35 34 33 32 31 30 29
[1] 37 38 39 40 41 42 43 44 45
[1] 55 54 53 52 51 50 49 48 47 46
I wasn't clear on whether you want rows until the row that contains 100 or whether you want 100 rows. Below I have assumed the first of these but if you want 100 rows remove the line that sets k and replace the computation of n (which we derive from k) with n <- 100.
1) n rows can accommodate numbers up to k = choose(n+1, 2) = n*(n+1)/2 so using k=100 and inverting the quadratic 0.5 * n^2 + 0.5 * n - k we derive n. Then use rep to create a grouping vector g equal to c(1, 2, 2, 3, 3, 3, ...) Finally split seq_along(g) by g.
k <- 100 # input
n <- ceiling((-.5 + sqrt(.5^2 - 4*.5*(-k))) / (2*.5)) # no of rows
# if you want 100 rows instead of rows until 100
# remove lines above and uncomment next line
# n <- 100
g <- rep(1:n, 1:n)
s <- split(seq_along(g), g)
s
giving this list:
$`1`
[1] 1
$`2`
[1] 2 3
$`3`
[1] 4 5 6
$`4`
[1] 7 8 9 10
$`5`
[1] 11 12 13 14 15
$`6`
[1] 16 17 18 19 20 21
$`7`
[1] 22 23 24 25 26 27 28
$`8`
[1] 29 30 31 32 33 34 35 36
$`9`
[1] 37 38 39 40 41 42 43 44 45
$`10`
[1] 46 47 48 49 50 51 52 53 54 55
$`11`
[1] 56 57 58 59 60 61 62 63 64 65 66
$`12`
[1] 67 68 69 70 71 72 73 74 75 76 77 78
$`13`
[1] 79 80 81 82 83 84 85 86 87 88 89 90 91
$`14`
[1] 92 93 94 95 96 97 98 99 100 101 102 103 104 105
2) Another possibility, using n from above, is:
m <- NA * diag(n)
t(replace(m, upper.tri(m, TRUE), 1:choose(n+1, 2)))
giving this matrix:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] 1 NA NA NA NA NA NA NA NA NA NA NA NA NA
[2,] 2 3 NA NA NA NA NA NA NA NA NA NA NA NA
[3,] 4 5 6 NA NA NA NA NA NA NA NA NA NA NA
[4,] 7 8 9 10 NA NA NA NA NA NA NA NA NA NA
[5,] 11 12 13 14 15 NA NA NA NA NA NA NA NA NA
[6,] 16 17 18 19 20 21 NA NA NA NA NA NA NA NA
[7,] 22 23 24 25 26 27 28 NA NA NA NA NA NA NA
[8,] 29 30 31 32 33 34 35 36 NA NA NA NA NA NA
[9,] 37 38 39 40 41 42 43 44 45 NA NA NA NA NA
[10,] 46 47 48 49 50 51 52 53 54 55 NA NA NA NA
[11,] 56 57 58 59 60 61 62 63 64 65 66 NA NA NA
[12,] 67 68 69 70 71 72 73 74 75 76 77 78 NA NA
[13,] 79 80 81 82 83 84 85 86 87 88 89 90 91 NA
[14,] 92 93 94 95 96 97 98 99 100 101 102 103 104 105
purrr style
imap(1:10, ~seq.int((.x-1)*.x/2 +1, length.out = .y))
[[1]]
[1] 1
[[2]]
[1] 2 3
[[3]]
[1] 4 5 6
[[4]]
[1] 7 8 9 10
[[5]]
[1] 11 12 13 14 15
[[6]]
[1] 16 17 18 19 20 21
[[7]]
[1] 22 23 24 25 26 27 28
[[8]]
[1] 29 30 31 32 33 34 35 36
[[9]]
[1] 37 38 39 40 41 42 43 44 45
[[10]]
[1] 46 47 48 49 50 51 52 53 54 55
Or some concatenated style if it'd be useful
imap_chr(1:10, ~ paste(seq.int((.x-1)*.x/2 +1, length.out = .y), collapse = " "))
[1] "1"
[2] "2 3"
[3] "4 5 6"
[4] "7 8 9 10"
[5] "11 12 13 14 15"
[6] "16 17 18 19 20 21"
[7] "22 23 24 25 26 27 28"
[8] "29 30 31 32 33 34 35 36"
[9] "37 38 39 40 41 42 43 44 45"
[10] "46 47 48 49 50 51 52 53 54 55"
Updated
There was a minor glitch in my code that I just fixed.
Here is another way of producing your desired result. I decided to turn it into a function so you can choose the number of rows:
f <- function(n = 10) {
out <- c(1)
print(out)
for(i in 2:n) {
result <- seq(out[length(out)] + 1, length = i)
out <- c(out, result)
print(result)
}
}
f(15)
[1] 1
[1] 2 3
[1] 4 5 6
[1] 7 8 9 10
[1] 11 12 13 14 15
[1] 16 17 18 19 20 21
[1] 22 23 24 25 26 27 28
[1] 29 30 31 32 33 34 35 36
[1] 37 38 39 40 41 42 43 44 45
[1] 46 47 48 49 50 51 52 53 54 55
[1] 56 57 58 59 60 61 62 63 64 65 66
[1] 67 68 69 70 71 72 73 74 75 76 77 78
[1] 79 80 81 82 83 84 85 86 87 88 89 90 91
[1] 92 93 94 95 96 97 98 99 100 101 102 103 104 105
[1] 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

Fill NA while keeping continous scale [duplicate]

This question already has answers here:
How to replace NA values in a data.table with na.spline
(2 answers)
How to replace NA (missing values) in a data frame with neighbouring values
(3 answers)
Closed 2 years ago.
I'd like to know if there's a way to fill NA values while keeping a continuous scale for a numeric vector.
Suppose I have a vector like this:
set.seed(55)
as.list(missForest::prodNA(data.frame(a=c(1:100)),noNA=0.3))
$a
[1] 1 NA 3 NA 5 NA 7 8 9 10 11 12 13 14 15 16 17 18 19 NA
[21] 21 22 23 24 NA 26 27 28 29 30 31 32 33 NA 35 NA 37 38 39 40
[41] 41 42 43 NA 45 46 47 48 NA 50 51 52 53 54 55 56 57 NA NA 60
[61] 61 62 NA NA 65 66 NA NA NA NA NA NA NA 74 75 NA 77 NA 79 NA
[81] 81 82 NA 84 85 86 NA 88 89 90 91 92 NA 94 95 NA NA NA NA 100
How can I get
> as.list(data.frame(a=c(1:100)))
$a
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[21] 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
[41] 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
[61] 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
[81] 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
by filling NA?
You can use zoo's na.spline
x <- missForest::prodNA(data.frame(a=c(1:100)),noNA=0.3)$a
zoo::na.spline(x)
#[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#[16] 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#[31] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#[46] 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#[61] 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#[76] 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#[91] 91 92 93 94 95 96 97 98 99 100

R, shifting parts of rows up [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I shift the data under ChangeJanAug from row 21 up, in order that the NA are filled with the correct numbers? Since I do not want to shift all the rows, I have to clue what to do.
city latitude JanTemp AprTemp AugTemp ChangeJanAug
1 MiamiFL 26 67 75 83 NA
2 HoustonTX 30 50 68 82 NA
3 MobileAL 31 50 68 82 NA
4 DallasTX 33 43 66 85 NA
5 PhoenixAZ 33 54 70 92 NA
6 LosAngelesCA 34 58 63 75 NA
7 MemphisTN 35 40 63 81 NA
8 NorfolkVA 37 39 57 77 NA
9 SanFranciscoCA 38 49 56 64 NA
10 BaltimoreMD 39 32 53 76 NA
11 KansasCityMO 39 28 55 76 NA
12 WashingtonDC 39 31 53 74 NA
13 PittsburghPA 40 25 50 71 NA
14 ClevelandOH 41 25 48 70 NA
15 NewYorkNY 41 32 53 76 NA
16 BostonMA 42 29 48 72 NA
17 SyracuseNY 43 22 46 68 NA
18 MinneapolisMN 45 12 46 71 NA
19 PortlandOR 46 40 51 69 NA
20 DuluthMN 47 7 39 64 NA
21 <NA> NA NA NA NA 16
22 <NA> NA NA NA NA 32
23 <NA> NA NA NA NA 32
24 <NA> NA NA NA NA 42
25 <NA> NA NA NA NA 38
26 <NA> NA NA NA NA 17
27 <NA> NA NA NA NA 41
28 <NA> NA NA NA NA 38
29 <NA> NA NA NA NA 15
30 <NA> NA NA NA NA 44
31 <NA> NA NA NA NA 48
32 <NA> NA NA NA NA 43
33 <NA> NA NA NA NA 46
34 <NA> NA NA NA NA 45
35 <NA> NA NA NA NA 44
36 <NA> NA NA NA NA 43
37 <NA> NA NA NA NA 46
38 <NA> NA NA NA NA 59
39 <NA> NA NA NA NA 29
40 <NA> NA NA NA NA 57
Thank you so much!
I agree with the comment of #Heroka that it would have been better to avoid such a situation. But now that you have the data in this form, you could use the following line of code to shift up the entries of the column ChangeJanAug by 20 rows:
df$ChangeJanAug <- c(df$ChangeJanAug[21:nrow(df)],rep(NA,(nrow(df)-20)))
Afterwards you could "clean up" the block of NA entries with
df <- df[1:20,]
If you plan to remove the NAs like this, you may not need to bother about vector recycling and you could use simply
df$ChangeJanAug <- df$ChangeJanAug[21:nrow(df)]
in the first step.
This could be an option
data$ChangeJanAug_new = c(data$ChangeJanAug[-(seq(20))], rep(NA, 20))
out = data[colnames(data) != "ChangeJanAug"]
#later if you want to remove NAs you could do this
out[!is.na(out$ChangeJanAug_new),]
Using na.omit and cbind you could do this (Given you original data is exactly as you mentioned in the question)
cbind(na.omit(data[,-6]), ChangeJanAug = na.omit(data$ChangeJanAug))
# city latitude JanTemp AprTemp AugTemp ChangeJanAug
#1 MiamiFL 26 67 75 83 16
#2 HoustonTX 30 50 68 82 32
#3 MobileAL 31 50 68 82 32
#4 DallasTX 33 43 66 85 42
#5 PhoenixAZ 33 54 70 92 38
#6 LosAngelesCA 34 58 63 75 17
#7 MemphisTN 35 40 63 81 41
#8 NorfolkVA 37 39 57 77 38
#9 SanFranciscoCA 38 49 56 64 15
#10 BaltimoreMD 39 32 53 76 44
#11 KansasCityMO 39 28 55 76 48
#12 WashingtonDC 39 31 53 74 43
#13 PittsburghPA 40 25 50 71 46
#14 ClevelandOH 41 25 48 70 45
#15 NewYorkNY 41 32 53 76 44
#16 BostonMA 42 29 48 72 43
#17 SyracuseNY 43 22 46 68 46
#18 MinneapolisMN 45 12 46 71 59
#19 PortlandOR 46 40 51 69 29
#20 DuluthMN 47 7 39 64 57

Apply NA to the rows that meet a condition in R

I have a data.frame like such:
set.seed(126)
df <- data.frame(a=sample(c(1:100, NA), 10), b=sample(1:100, 10), c=sample(1:100, 10))
a b c
1 65 48 19
2 46 15 80
3 NA 47 84
4 68 34 46
5 23 75 42
6 92 87 68
7 79 28 48
8 84 55 9
9 28 43 38
10 94 99 77
>
I'd like to write a function that transforms all values in all columns to NA if df$a is NA However, I don't want to just assign b and c the value of NA, rather I would like a function that turns all columns in the data.frame to NA if the condition is.na(a) is met, no matter the number of columns.
I think you are just looking for
df[is.na(df$a), ] <- NA
# a b c
# 1 65 48 19
# 2 46 15 80
# 3 NA NA NA
# 4 68 34 46
# 5 23 75 42
# 6 92 87 68
# 7 79 28 48
# 8 84 55 9
# 9 28 43 38
# 10 94 99 77

Resources