简体   繁体   English

求和 R 中矩阵每一列的最后 n 个非 NA 值

[英]Sum the last n non NA values in each column of a matrix in R

I have a matrix that looks like below:我有一个如下所示的矩阵:

x1<-c(1,2,3,4,5,6,NA)
x2<-c(1,2,NA,4,5,NA,NA)
x3<-c(1,2,3,4,NA,NA,NA)
x4<-c(1,2,3,NA,NA,NA,NA)
x5<-c(1,2,NA,NA,NA,NA,NA)
x<-cbind(x1,x2,x3,x4,x5)

If I want to calculate the last 3 non NA values of each column, and if a column has less than 3 non NA values (like column 5), then I'll sum all the non NA values in that column.如果我想计算每列的最后 3 个非 NA 值,并且如果一列的非 NA 值少于 3 个(如第 5 列),那么我将对该列中的所有非 NA 值求和。 I want an output that looks like我想要一个 output 看起来像

15 11 10 6 3

Thank you!谢谢!

You can use apply with tail to sum up the last non NA like:您可以使用apply with tail来总结最后一个非NA ,例如:

apply(x, 2, function(x) sum(tail(x[!is.na(x)], 3)))
#x1 x2 x3 x4 x5 
#15 11  9  6  3 

It also works with a customized function (@GKi answer is pretty cool):它还适用于自定义的 function(@GKi 回答很酷):

#Build function
myfun <- function(y)
{
  #Count na
  i <- length(which(!is.na(y)))
  if(i<3)
  {
    r1 <- sum(y,na.rm=T)
  } else
  {
    y1 <- y[!is.na(y)]
    y2 <- y1[(length(y1)-2):length(y1)]
    r1 <- sum(y2)
  }
  return(r1)
}
#Apply
apply(x,2,myfun)

Output: Output:

x1 x2 x3 x4 x5 
15 11  9  6  3

One dplyr option using the logic from @GKi could be:使用来自@GKi 的逻辑的一个dplyr选项可能是:

x %>%
 data.frame() %>%
 summarise(across(everything(), ~ sum(tail(na.omit(.), 3))))

  x1 x2 x3 x4 x5
1 15 11  9  6  3

Or:要么:

x %>%
 data.frame() %>%
 summarise(across(everything(), ~ sum(rev(na.omit(.))[1:3], na.rm = TRUE)))

Using sapply from base R使用来自sapplybase R

sapply(as.data.frame(x), function(x) sum(tail(na.omit(x), 3)))
# x1 x2 x3 x4 x5 
#15 11  9  6  3 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM