英文:
Sum a number to each element of an array
问题
我想对数组的每个元素加5(不涉及所有维度),而不使用循环。结果,我希望得到一个具有完全相同维度的数组,但每个元素都应加5。我尝试使用sum和apply函数,但要么得到每个元素的总和(一个数字),要么出现以下错误消息:
Error in apply(b[, , , 2], 5, FUN = sum, na.rm = T) :
'MARGIN' does not match dim(X)
因为我的数据集中有很多NA,所以我需要na.rm = T,所以我希望得到NA + 5 = 5。
这里有一个示例,我试图对所有维度加5,但不包括[,,,1]
:
b <- array(seq(1,48,1), dim = c(4,2,3,2))
b[4,2,3,2] <- NA
我尝试过:
apply(b[,,,2], 5, FUN = sum, na.rm = T)
sum(b[,,,2], 5, na.rm = T)
我期望的答案是:
(a <- array(c(seq(1,24,1),seq(25,47,1)+5, 5), dim = c(4,2,3,2)))
英文:
I would like to sum 5 to each element of an array (not through all dimensions) without using a loop for. As the result, I want to get an array with the exact same dimensions, but each element should have 5 added to it. I tried using the sum and the apply functions, but I either get the sum of each of the elements (one number), or this error message:
Error in apply(b[, , , 2], 5, FUN = sum, na.rm = T) :
'MARGIN' does not match dim(X)
I need to have na.rm = T, because there are a lot of NA in my dataset, so I want to get NA + 5 = 5
Here is an example where I'm trying to sum 5 to all dimensions but to [,,,1]
:
b <- array(seq(1,48,1), dim = c(4,2,3,2))
b[4,2,3,2] <- NA
I tried:
sum(b[,,,2], 5, na.rm = T)
I was expecting to get as an answer:
(a <- array(c(seq(1,24,1),seq(25,47,1)+5, 5), dim = c(4,2,3,2)))
答案1
得分: 3
R默认支持将值添加到矩阵和数组中。您可以使用 +
运算符将值添加到向量/
英文:
R by default supports adding to matricies and arrays. You can just use the +
operator to add a value to each element of a vector/matrix/array. Since you only want to affect parts of that array, just use square brackets to subset the parts you want to affect:
b[,,,2] <- b[,,,2] + 5
table(b==a, useNA='always')
TRUE <NA>
47 1
You have NA
values in your array. In R, by default, operations involving an NA
return NA
. So in this case, the NA
values in b
will remain NA
after the addition.
If you want different behavior, for example if you want NA + 5 = 5
, then you need to convert the NA values to 0 in the parts of the array you care about before doing the addition
To replace all NAs with 0:
b[is.na(b)] <- 0
To only do that in b[,,,2]
:
b[,,,2][is.na(b[,,,2])] <- 0
答案2
得分: 1
We can do
replace(b, is.na(b), 0) + 5
, , 1, 1
[,1] [,2]
[1,] 6 10
[2,] 7 11
[3,] 8 12
[4,] 9 13
, , 2, 1
[,1] [,2]
[1,] 14 18
[2,] 15 19
[3,] 16 20
[4,] 17 21
, , 3, 1
[,1] [,2]
[1,] 22 26
[2,] 23 27
[3,] 24 28
[4,] 25 29
, , 1, 2
[,1] [,2]
[1,] 30 34
[2,] 31 35
[3,] 32 36
[4,] 33 37
, , 2, 2
[,1] [,2]
[1,] 38 42
[2,] 39 43
[3,] 40 44
[4,] 41 45
, , 3, 2
[,1] [,2]
[1,] 46 50
[2,] 47 51
[3,] 48 52
[4,] 49 5
Disclaimer: 此答案涉及“我想将数组的每个元素加5”,其中NA
应被视为0
。
英文:
We can do
replace(b, is.na(b), 0) + 5
# , , 1, 1
#
# [,1] [,2]
# [1,] 6 10
# [2,] 7 11
# [3,] 8 12
# [4,] 9 13
#
# , , 2, 1
#
# [,1] [,2]
# [1,] 14 18
# [2,] 15 19
# [3,] 16 20
# [4,] 17 21
#
# , , 3, 1
#
# [,1] [,2]
# [1,] 22 26
# [2,] 23 27
# [3,] 24 28
# [4,] 25 29
#
# , , 1, 2
#
# [,1] [,2]
# [1,] 30 34
# [2,] 31 35
# [3,] 32 36
# [4,] 33 37
#
# , , 2, 2
#
# [,1] [,2]
# [1,] 38 42
# [2,] 39 43
# [3,] 40 44
# [4,] 41 45
#
# , , 3, 2
#
# [,1] [,2]
# [1,] 46 50
# [2,] 47 51
# [3,] 48 52
# [4,] 49 5
Disclaimer: This answer refers to "I would like to sum 5 to each element of an array" where NA
should be considered a 0
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论